Docker, CI/CD with GitHub Actions and Deployment

Dot NET Programming 17 min min read Updated: Nov 16, 2026 Advanced
Docker, CI/CD with GitHub Actions and Deployment
Advanced Topic 3 of 3

Step 1: Publish

bash dotnet publish -c Release -o ./publish # run it dotnet ./publish/ShopApi.dll
Publish optionMeaning
Framework-dependent (default)Small. Target machine needs the .NET runtime.
--self-containedIncludes the runtime. Larger, runs anywhere.
-p:PublishSingleFile=trueOne executable file
Native AOTVery fast start and small memory. Some features limited.

Step 2: Dockerfile (Multi-Stage)

dockerfile # ---- build stage ---- FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build WORKDIR /src COPY *.csproj ./ RUN dotnet restore COPY . . RUN dotnet publish -c Release -o /app/publish --no-restore # ---- runtime stage ---- FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final WORKDIR /app COPY --from=build /app/publish . EXPOSE 8080 ENTRYPOINT ["dotnet", "ShopApi.dll"]
Why two stages? The SDK image is large. The final image contains only the runtime and your published files, so it is smaller and safer. Official .NET images listen on port 8080 and run as a non-root user.
text # .dockerignore bin/ obj/ .git/ *.user
bash docker build -t shopapi . docker run -p 8080:8080 -e ASPNETCORE_ENVIRONMENT=Production shopapi

Step 3: Configuration with Environment Variables

Use double underscores to reach nested settings:

bash -e ConnectionStrings__Default="Host=db;Database=shop;Username=app;Password=secret" -e Jwt__Key="long-random-secret"

Step 4: docker compose with a Database

yaml services: api: build: . ports: - "8080:8080" environment: ConnectionStrings__Default: "Host=db;Database=shop;Username=app;Password=${DB_PASSWORD}" depends_on: db: condition: service_healthy db: image: postgres:17 environment: POSTGRES_USER: app POSTGRES_PASSWORD: ${DB_PASSWORD} POSTGRES_DB: shop volumes: - dbdata:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U app -d shop"] interval: 5s retries: 10 volumes: dbdata:

Step 5: CI with GitHub Actions

Every push builds the code and runs the tests. Create .github/workflows/ci.yml:

yaml ame: CI on: push: branches: [ main ] pull_request: jobs: build-test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-dotnet@v4 with: dotnet-version: 10.0.x - run: dotnet restore - run: dotnet build --no-restore -c Release - run: dotnet test --no-build -c Release --verbosity normal docker: needs: build-test if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v4 - uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - uses: docker/build-push-action@v6 with: context: . push: true tags: ghcr.io/${{ github.repository }}:latest

Step 6: Where to Host

OptionGood for
Azure App ServiceEasiest path for .NET, built-in scaling and slots
Azure Container Apps / AWS ECS / Google Cloud RunContainers without managing servers
VPS with Docker + NginxCheap and full control
KubernetesLarge systems with many services

Production Checklist

  • ASPNETCORE_ENVIRONMENT=Production
  • HTTPS terminated at the load balancer or reverse proxy (use forwarded headers)
  • Secrets from environment or a vault
  • Health check endpoint (/health) configured in the host
  • Database migrations applied as a deploy step, not at random app start-up in a multi-server setup
  • Logs sent to a central place
  • Backups tested
Migrations: Running Database.Migrate() at start-up is fine for small apps but risky with several instances starting at once. For larger systems generate an SQL script or run a migration bundle (dotnet ef migrations bundle) in the pipeline.

Conclusion

Publish the app, put it in a multi-stage Docker image, run tests in GitHub Actions and deploy with environment-based configuration. Add health checks and central logs from day one.

Quick Summary: dotnet publish -> Docker image -> CI tests -> deploy with env vars.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators