☁ Azure Container Guide

3. Build the container image

Package your ASP.NET Core app as an image and verify it runs locally before touching Azure.

Write a Dockerfile

A multi-stage build keeps the final image small: the SDK image compiles the app, and only the published output is copied into the lightweight runtime image.

# Build stage
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY MyApp.csproj ./
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app/publish --no-restore /p:UseAppHost=false

# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final
WORKDIR /app
COPY --from=build /app/publish .
ENV ASPNETCORE_HTTP_PORTS=8080
ENV ASPNETCORE_FORWARDEDHEADERS_ENABLED=true
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApp.dll"]
Port 8080: Since .NET 8, the official ASP.NET images listen on port 8080 (not 80) and run as a non-root user. Remember this number: Azure must be told which port your container listens on.

Add a .dockerignore

**/bin/
**/obj/
**/.vs/
**/.git/
Dockerfile
.dockerignore

Build and run locally

Run these from the folder containing the Dockerfile:

docker build -t azure-container-guide:local .
docker run --rm -p 8080:8080 azure-container-guide:local

Browse to http://localhost:8080 to confirm the site responds.

Handle HTTPS correctly behind Azure

Azure terminates TLS before traffic reaches your container, so the container sees plain HTTP. Tell ASP.NET Core to trust the X-Forwarded-For and X-Forwarded-Proto headers so redirects and absolute URLs keep the original https scheme. The simplest way in a container is an environment variable (already included in the Dockerfile above):

ENV ASPNETCORE_FORWARDEDHEADERS_ENABLED=true

Alternative: no Dockerfile

The .NET SDK can build a container image directly:

dotnet publish --os linux --arch x64 -t:PublishContainer -p:ContainerRepository=azure-container-guide

Using Visual Studio 2026

  1. Right-click the project, then Add → Docker Support and choose Linux.
  2. Pick the Container (Dockerfile) debug profile and press F5 to run inside Docker.
Apple silicon or ARM machines: Azure hosts typically run linux/amd64 images. Build with docker build --platform linux/amd64 ..., or build in the cloud with az acr build (next step).