-
-
Notifications
You must be signed in to change notification settings - Fork 6
Modernize Docker Environment: Security Hardening and Multi-Arch CI #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
xuara
wants to merge
15
commits into
dkorecko:main
Choose a base branch
from
xuara:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c0b9f9f
security: harden Dockerfile and modernize CI/CD workflows (#140) (#1)
xuara 32b3386
ci: enforce lowercase repository naming for Docker tags
xuara e18c239
ci: fix lowercase naming requirement for release images
xuara 5f7c0be
feat: harden Dockerfile with multi-stage builds and non-root security
xuara d95d465
ci: optimize unstable workflow with 2026 standards and smoke tests
xuara de17f0c
test: optimize multi-arch smoke test to prevent emulation timeouts
xuara 024b6ab
fix: YAML indentation
xuara ca5306d
test: implement robust timeout for multi-arch smoke tests
xuara 8a6639e
ci: enable multi-arch validation in docker-build-check
xuara 01dfbce
ci: add qemu setup to release workflow for arm support
xuara 183ff42
ci: optimize release workflow with build caching
xuara 6b72722
refactor: modernize dockerfile for .NET 10 on Ubuntu 26.04 LTS
xuara 8a2b963
ci: use 'released' trigger for stable tags
xuara b647301
refactor: harden dockerfile architecture and permission logic
xuara 6bc375d
chore: add .dockerignore to optimize docker build context
xuara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| **/%* | ||
| **/bin/ | ||
| **/obj/ | ||
| .git/ | ||
| .vs/ | ||
| .vscode/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| name: Docker Build Check | ||
|
|
||
| on: [push, pull_request, workflow_dispatch] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up QEMU | ||
| uses: docker/setup-qemu-action@v3 | ||
|
|
||
| - name: Set up Docker Buildx | ||
| uses: docker/setup-buildx-action@v4 | ||
|
|
||
| - name: Build Docker image | ||
| uses: docker/build-push-action@v7 | ||
| with: | ||
| context: . | ||
| platforms: linux/amd64,linux/arm64,linux/arm/v7 | ||
| file: ./Dockerfile | ||
| push: false # We only want to test the build, not push it | ||
| tags: patchpanda:test | ||
| cache-from: type=gha | ||
| cache-to: type=gha,mode=max | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,49 @@ | ||
| name: Build and Publish Docker image to GHCR | ||
| name: Build and publish Docker image | ||
|
|
||
| on: | ||
| release: | ||
| types: [published] | ||
| types: [released] | ||
|
|
||
| jobs: | ||
| build-and-push: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| packages: write | ||
| id-token: write # Required for security attestations | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Lowercase repo name | ||
| run: echo "REPO_LC=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV | ||
|
|
||
| - name: Set up QEMU | ||
| uses: docker/setup-qemu-action@v3 | ||
|
|
||
| - name: Set up Docker Buildx | ||
| uses: docker/setup-buildx-action@v3 | ||
| with: | ||
| platforms: linux/amd64,linux/arm64,linux/arm/v7 | ||
| uses: docker/setup-buildx-action@v4 | ||
|
|
||
| - name: Log in to GitHub Container Registry | ||
| uses: docker/login-action@v3 | ||
| uses: docker/login-action@v4 | ||
| with: | ||
| registry: ghcr.io | ||
| username: ${{ secrets.GHCR_USERNAME }} | ||
| password: ${{ secrets.GHCR_TOKEN }} | ||
|
|
||
| - name: Extract release version | ||
| id: vars | ||
| run: echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV | ||
|
|
||
| - name: Set lowercase repository name | ||
| id: repo-name | ||
| run: echo "REPO_LOWER=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Build and push Docker image | ||
| uses: docker/build-push-action@v5 | ||
| uses: docker/build-push-action@v7 | ||
| with: | ||
| context: . | ||
| platforms: linux/amd64,linux/arm64,linux/arm | ||
| platforms: linux/amd64,linux/arm64,linux/arm/v7 | ||
| file: ./Dockerfile | ||
| push: true | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| build-args: | | ||
| RELEASE_VERSION=${{ env.RELEASE_VERSION }} | ||
| RELEASE_VERSION=${{ github.event.release.tag_name }} | ||
| tags: | | ||
| ghcr.io/${{ env.REPO_LOWER }}:${{ env.RELEASE_VERSION }} | ||
| ghcr.io/${{ env.REPO_LOWER }}:latest | ||
| ghcr.io/${{ env.REPO_LC }}:latest | ||
| ghcr.io/${{ env.REPO_LC }}:${{ github.event.release.tag_name }} | ||
| provenance: true | ||
| sbom: true | ||
| cache-from: type=gha | ||
| cache-to: type=gha,mode=max | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,99 @@ | ||
| # Declare ARGs for build platform and target architecture for clarity | ||
| # PatchPanda.Web - Multi-Stage Docker Build (2026 Standards) | ||
| # Support: amd64, arm64, arm/v7 | ||
| # Features: Non-root user, Security Patches, Docker-in-Docker CLI, OIDC Ready, Health Checks | ||
|
|
||
| ARG BUILDPLATFORM | ||
|
|
||
| # ============================================================================ | ||
| # STAGE 1: Base Runtime (Hardened) | ||
| # ============================================================================ | ||
| FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base | ||
|
|
||
| WORKDIR /app | ||
| EXPOSE 8080 | ||
|
|
||
| # Patch OS vulnerabilities and install basic dependencies | ||
| USER root | ||
| RUN apt-get update && \ | ||
| apt-get upgrade -y && \ | ||
| apt-get install -y --no-install-recommends curl ca-certificates gnupg && \ | ||
| rm -rf /var/lib/apt/lists/* | ||
| USER app | ||
|
|
||
| # ============================================================================ | ||
| # STAGE 2: Build (Cross-Platform) | ||
| # ============================================================================ | ||
| FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:10.0 AS build | ||
|
|
||
| ARG TARGETARCH | ||
| ARG TARGETVARIANT | ||
| WORKDIR /src | ||
|
|
||
| # Copy project file and restore specifically for the target architecture | ||
| COPY ["PatchPanda.Web/PatchPanda.Web.csproj", "PatchPanda.Web/"] | ||
|
|
||
| # Map Docker's TARGETARCH to the arch used in .NET RIDs and restore dependencies | ||
| RUN export DOTNET_ARCH=$(case ${TARGETARCH} in \ | ||
| "amd64") echo "x64" ;; \ | ||
| "arm64") echo "arm64" ;; \ | ||
| "arm") echo "arm" ;; \ | ||
| *) echo "Unsupported architecture: ${TARGETARCH}"; exit 1 ;; \ | ||
| esac) && \ | ||
| dotnet restore "PatchPanda.Web/PatchPanda.Web.csproj" -r "linux-${DOTNET_ARCH}" | ||
| "amd64") echo "x64" ;; \ | ||
| "arm64") echo "arm64" ;; \ | ||
| "arm") [ "${TARGETVARIANT}" = "v7" ] && echo "arm" || { echo "ERROR: Unsupported ARM variant '${TARGETVARIANT}'"; exit 1; } ;; \ | ||
| *) echo "ERROR: Unsupported architecture '${TARGETARCH}'"; exit 1 ;; \ | ||
| esac) && \ | ||
| dotnet restore "PatchPanda.Web/PatchPanda.Web.csproj" \ | ||
| --runtime "linux-${DOTNET_ARCH}" | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| # Copy remaining source code | ||
| COPY . . | ||
|
|
||
| # Build and publish the application for the target runtime | ||
| # Publish the Release binary with strict ARMv7 validation | ||
| RUN export DOTNET_ARCH=$(case ${TARGETARCH} in \ | ||
| "amd64") echo "x64" ;; \ | ||
| "arm64") echo "arm64" ;; \ | ||
| "arm") echo "arm" ;; \ | ||
| esac) && \ | ||
| dotnet publish "PatchPanda.Web/PatchPanda.Web.csproj" \ | ||
| -c Release \ | ||
| -o /app/publish \ | ||
| -r "linux-${DOTNET_ARCH}" \ | ||
| --no-restore \ | ||
| --self-contained false | ||
| "amd64") echo "x64" ;; \ | ||
| "arm64") echo "arm64" ;; \ | ||
| "arm") [ "${TARGETVARIANT}" = "v7" ] && echo "arm" || { echo "ERROR: Unsupported ARM variant '${TARGETVARIANT}'"; exit 1; } ;; \ | ||
| *) echo "ERROR: Unsupported architecture '${TARGETARCH}'"; exit 1 ;; \ | ||
| esac) && \ | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| dotnet publish "PatchPanda.Web/PatchPanda.Web.csproj" \ | ||
| --configuration Release \ | ||
| --output /app/publish \ | ||
| --runtime "linux-${DOTNET_ARCH}" \ | ||
| --no-restore \ | ||
| --self-contained false | ||
|
|
||
| # ============================================================================ | ||
| # STAGE 3: Final Production Image (Ubuntu 26.04 LTS "Resolute") | ||
| # ============================================================================ | ||
| FROM base AS final | ||
| # Install utilities needed for installing the Docker CLI | ||
| RUN apt-get update && \ | ||
| apt-get install -y \ | ||
| ca-certificates \ | ||
| curl \ | ||
| gnupg \ | ||
| lsb-release && \ | ||
| \ | ||
| # Add Docker's official GPG key | ||
| mkdir -m 0755 -p /etc/apt/keyrings && \ | ||
| curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \ | ||
| \ | ||
| # Set up the stable repository, explicitly using 'bookworm' (Debian 12) | ||
| echo \ | ||
| "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \ | ||
| bookworm stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null && \ | ||
| \ | ||
| # Install the Docker CLI only | ||
| apt-get update && \ | ||
| apt-get install -y docker-ce-cli && \ | ||
| \ | ||
| # Clean up | ||
| rm -rf /var/lib/apt/lists/* | ||
| WORKDIR /app | ||
| COPY --from=build /app/publish . | ||
|
|
||
| # Set up environment and diagnostics | ||
| ARG RELEASE_VERSION | ||
| ARG ENABLE_DIAGNOSTICS=0 | ||
| ENV APP_VERSION=$RELEASE_VERSION | ||
| LABEL version=$RELEASE_VERSION | ||
| ENV DOTNET_EnableDiagnostics=${ENABLE_DIAGNOSTICS} | ||
|
|
||
| # Install Docker CLI using Ubuntu 'resolute' repo | ||
| USER root | ||
| RUN mkdir -m 0755 -p /etc/apt/keyrings && \ | ||
| curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \ | ||
| chmod a+r /etc/apt/keyrings/docker.gpg && \ | ||
| echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu resolute stable" > /etc/apt/sources.list.d/docker.list && \ | ||
| apt-get update && \ | ||
| apt-get install -y --no-install-recommends docker-ce-cli && \ | ||
| rm -rf /var/lib/apt/lists/* | ||
|
|
||
| WORKDIR /app | ||
| # Copy artifacts first (owned by root for security) | ||
| COPY --from=build /app/publish . | ||
|
|
||
| # Create data directory and fix permissions AFTER artifacts are copied | ||
| # Narrowed chown ensures binaries stay read-only while data is writable | ||
| RUN mkdir -p /app/data && \ | ||
| chown -R app:app /app/data | ||
|
|
||
| HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \ | ||
| CMD curl -f http://localhost:8080/ || exit 1 | ||
|
|
||
| LABEL version=$RELEASE_VERSION \ | ||
| description="PatchPanda Web Application" \ | ||
| maintainer="dkorecko" | ||
|
|
||
| USER app | ||
| ENTRYPOINT ["dotnet", "PatchPanda.Web.dll"] | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.