Docker, Azure and CI for .NET
Containers package a .NET app with its runtime so it runs the same everywhere, Azure App Service hosts it without managing servers, and a GitHub Actions pipeline tests, publishes and deploys on every push. The .NET specifics to know are port 8080, multi-stage images and SDK container publish.
70.1 Why is it used?
- In the demo a simple Dockerfile builds and the container runs, but curl gets "Empty reply from server".
- The logs show Kestrel listening on 8080 while the mapping went to port 80.
- The single-stage image ships the whole SDK: 1.3 GB.
- Fixes: map 8080, use a multi-stage Dockerfile (or PublishContainer), and automate it all with CI.
FROM mcr.microsoft.com/dotnet/sdk:10.0WORKDIR /srcCOPY . .RUN dotnet publish -c Release -o /appWORKDIR /appENTRYPOINT ["dotnet", "Shop.dll"]$ docker build -t shop-c70:naive .#10 naming to docker.io/library/shop-c70:naive done$ docker run -d -p 5070:80 --name shop-c70-a shop-c70:naive986d4bc4b452c1a210d2a43fc7cd7af8c5ecd8ff...$ curl -i localhost:5070curl: (52) Empty reply from server$ docker logs shop-c70-ainfo: Microsoft.Hosting.Lifetime[14] Now listening on: http://[::]:8080info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down.- Step 1
1.docker run -p 5070:80 maps host 5070 to container port 80
- Step 2
2..NET 8+ images set ASPNETCORE_HTTP_PORTS=8080
- Step 3
3.Kestrel listens on 8080; nobody listens on 80
- Step 4
4.curl localhost:5070 -> (52) Empty reply from server
- Step 5
5.Fix: -p 5070:8080 -> HTTP/1.1 200 OK
70.2 Multi-stage .NET Dockerfile
- Stage 1 (build) starts from the SDK image.
- Copy only Shop.csproj and run dotnet restore first, then copy the rest and publish with --no-restore.
- Stage 2 uses only the aspnet:10.0 runtime image and copies the published files with COPY --from=build.
- USER $APP_UID runs the app as a non-root user; EXPOSE 8080 documents the port.
# Stage 1: build (full SDK)FROM mcr.microsoft.com/dotnet/sdk:10.0 AS buildWORKDIR /srcCOPY Shop.csproj .RUN dotnet restoreCOPY . .RUN dotnet publish -c Release --no-restore -o /app # Stage 2: run (only ASP.NET runtime)FROM mcr.microsoft.com/dotnet/aspnet:10.0WORKDIR /appCOPY --from=build /app .USER $APP_UIDEXPOSE 8080ENTRYPOINT ["dotnet", "Shop.dll"]docker build -t shop-c70:multi .docker images shop-c70docker run -d -p 5070:8080 \ --name shop-c70-b shop-c70:multicurl -i localhost:5070$ docker images shop-c70IMAGE DISK USAGEshop-c70:multi 342MBshop-c70:naive 1.3GB$ docker run -d -p 5070:8080 --name shop-c70-b shop-c70:multifed5b9a5e282b2751615f79e6a43370d464643e4...$ curl -i localhost:5070HTTP/1.1 200 OKContent-Type: text/plain; charset=utf-8Server: Kestrel Shop API on .NET 10.0.14, Ubuntu 24.04.5 LTS, user app70.3 Layer caching
- Checkpoint: change one line in Program.cs and rebuild.
- COPY Shop.csproj and dotnet restore come from the cache; only COPY . . and publish run again.
- That is why the csproj is copied separately: packages are not downloaded on every code change.
- Add bin and obj to .dockerignore.
# only one line changed in Program.cs$ docker build -t shop-c70:multi .#9 [build 3/6] COPY Shop.csproj .#9 CACHED#10 [build 4/6] RUN dotnet restore#10 CACHED#11 [build 5/6] COPY . .#11 DONE 0.3s#12 [build 6/6] RUN dotnet publish -c Release --no-restore -o /app#12 DONE 21.5s70.4 dotnet publish /t:PublishContainer
- The .NET SDK builds container images itself, no Dockerfile needed.
- ContainerRepository = image name, ContainerImageTag = tag, ContainerFamily noble-chiseled = small base image.
- The image is pushed straight into local docker: 239 MB.
- Chiseled images have no shell (sh not found), so the attack surface is smaller.
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net10.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <ContainerRepository>shop-c70</ContainerRepository> <ContainerImageTag>1.0.0</ContainerImageTag> <ContainerFamily>noble-chiseled</ContainerFamily> </PropertyGroup></Project>$ dotnet publish /t:PublishContainer Shop -> D:\SkillUp\tmp-C70\Shop\bin\Release\net10.0\publish\ Building image 'shop-c70' with tags '1.0.0' on top of base image 'mcr.microsoft.com/dotnet/aspnet:10.0-noble-chiseled-extra'. Pushed image 'shop-c70:1.0.0' to local registry via 'docker'.$ docker images shop-c70IMAGE DISK USAGEshop-c70:1.0.0 239MBshop-c70:multi 342MB$ docker exec shop-c70-c shexec: "sh": executable file not found in $PATH| Image | How it is built | Size in the demo | Notes |
|---|---|---|---|
| shop-c70:naive | Single stage on the SDK image | 1.3GB | Whole SDK shipped; wrong port mapping |
| shop-c70:multi | Multi-stage: SDK builds, aspnet:10.0 runs | 342MB | Restore layer cached; non-root USER $APP_UID |
| shop-c70:1.0.0 | dotnet publish /t:PublishContainer, noble-chiseled | 239MB | No Dockerfile; no shell, smaller attack surface |
70.5 Azure App Service
- A managed platform: Azure handles the server, OS patches and scaling; you bring the app.
- Resource group -> App Service plan (Linux B1 decides size and price) -> web app with runtime DOTNETCORE:10.0.
- Deploy the published output as a zip with az webapp deploy.
- The recording had no Azure subscription, so these commands were not run; without login az says "Please run 'az login'".
- Docker is not required: App Service has the .NET runtime built in. Choose a container for custom OS packages or the same image everywhere.
# Needs an Azure account + az CLIaz loginaz group create -n shop-rg -l centralindiaaz appservice plan create -g shop-rg \ -n shop-plan --is-linux --sku B1az webapp create -g shop-rg -p shop-plan \ -n myshop-api --runtime "DOTNETCORE:10.0" # Publish the code, zip deploydotnet publish Shop -c Release -o publishcd publish && zip -r ../app.zip . && cd ..az webapp deploy -g shop-rg -n myshop-api \ --src-path app.zip --type zip# no Azure subscription in this recording$ az group create -n shop-rg -l centralindiaERROR: Please run 'az login' to setup account.70.6 GitHub Actions CI/CD
- .github/workflows/ci.yml runs on every push and pull_request.
- setup-dotnet installs the .NET 10 SDK; dotnet test restores, builds and tests; dotnet publish creates the output.
- upload-artifact keeps the output as "shop" for the deploy job.
- deploy: needs build (only after a green build), if main branch only (never from a PR).
- permissions id-token: write enables OIDC login, so no Azure password sits in a secret.
- actionlint reported 0 errors for the whole file.
name: dotnet-cion: [push, pull_request]jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: 10.0.x - run: dotnet test -c Release - run: dotnet publish Shop -c Release -o out - uses: actions/upload-artifact@v7 with: { name: shop, path: out }# same steps in an sdk:10.0 container$ dotnet test -c Release Shop -> /work/Shop/bin/Release/net10.0/Shop.dll Shop.Tests -> /work/Shop.Tests/bin/Release/net10.0/Shop.Tests.dllPassed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1$ dotnet publish Shop -c Release -o out Shop -> /work/out/ deploy: needs: build if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest permissions: { id-token: write } steps: - uses: actions/download-artifact@v8 with: { name: shop, path: out } - uses: azure/login@v3 with: client-id: ${{ secrets.AZ_CLIENT_ID }} tenant-id: ${{ secrets.AZ_TENANT_ID }} subscription-id: ${{ secrets.AZ_SUB_ID }} - uses: azure/webapps-deploy@v3 with: { app-name: myshop-api, package: out }$ actionlint -verbose .github/workflows/ci.ymlverbose: Found 0 parse errors in 1 ms for .github/workflows/ci.ymlverbose: Found total 0 errors in 63 ms for .github/workflows/ci.yml- Step 1
1.git push to GitHub (push or pull_request triggers dotnet-ci)
- Step 2
2.build job: setup-dotnet 10.0.x, dotnet test, dotnet publish
- Step 3
3.upload-artifact saves the published output as "shop"
- Step 4
4.deploy job: needs build, runs only on refs/heads/main
- Step 5
5.azure/login with OIDC (id-token: write), no password in secrets
- Step 6
6.azure/webapps-deploy sends the package to App Service myshop-api
70.7 Common mistakes
- Mapping container port 80: .NET 8+ images listen on 8080.
- Single-stage image on the SDK: huge image with build tools inside.
- COPY . . before restore: every code change re-downloads packages.
- Running as root in the container.
- CI SDK version lower than the project target: error NETSDK1045.
- Storing Azure passwords in secrets instead of using OIDC.
# Wrong: SDK 8, project targets net10.0- uses: actions/setup-dotnet@v6 with: dotnet-version: 8.0.x- run: dotnet test Shop.Tests -c Release# error NETSDK1045: The current .NET SDK# does not support targeting .NET 10.0. # Right: SDK = project target- uses: actions/setup-dotnet@v6 with: dotnet-version: 10.0.x- run: dotnet test Shop.Tests -c Release# Passed! - Failed: 0, Passed: 170.8 Interview tips
- Multi-stage Dockerfile: SDK image publishes, small aspnet image runs; or PublishContainer without a Dockerfile.
- Since .NET 8 containers use port 8080 and a non-root user (APP_UID).
- GitHub Actions: test + publish on every push; on main, OIDC login and deploy to App Service.
- App Service runs .NET directly from a zip deploy; containers are optional.
- Copy csproj and restore first to use the layer cache.