ASP.NET Core MVC Advanced

Running your app in Docker

A container packages your app with everything it needs to run, so it behaves the same on your laptop, in CI and in production. This lesson writes a multi-stage Dockerfile for an ASP.NET Core app, runs it with SQL Server using Docker Compose, and covers .NET's built-in container publishing.

From code to container
  1. StartBuild stageSDK image restores and publishes
  2. Step 2Final stageRuntime image gets only the output
  3. Step 3docker buildCreates the image
  4. Step 4docker compose upApp and SQL Server start together
  5. EndDeploy the imageSame image in test and production

Why containers

A container image holds the .NET runtime, your compiled app and its settings. Anyone with Docker can run it with one command, and the same image moves from test to production unchanged. Azure Container Apps, Kubernetes and most hosts run images directly.

A multi-stage Dockerfile

The first stage uses the large SDK image to build; the final stage copies only the output into the small runtime image. Official .NET 8+ images run as a non-root user and listen on port 8080.

Dockerfile
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY *.csproj ./
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final
WORKDIR /app
COPY --from=build /app .
USER $APP_UID
EXPOSE 8080
ENTRYPOINT ["dotnet", "Shop.Web.dll"]

Run it with a database

Docker Compose starts the app and SQL Server together. Settings come from environment variables; keep real passwords in an .env file that is not committed.

YAML
services:
  web:
    build: .
    ports:
      - "8080:8080"
    environment:
      ASPNETCORE_ENVIRONMENT: Development
      ConnectionStrings__Shop: "Server=db;Database=Shop;User Id=sa;Password=${SA_PASSWORD};TrustServerCertificate=True"
    depends_on:
      - db
  db:
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      ACCEPT_EULA: "Y"
      MSSQL_SA_PASSWORD: ${SA_PASSWORD}
    volumes:
      - sqldata:/var/opt/mssql
volumes:
  sqldata:

Tips

  • No Dockerfile needed: dotnet publish /t:PublishContainer builds an image straight from the project.
  • Add a .dockerignore with bin/ and obj/ so builds are fast and clean.
  • Copy the .csproj and restore before copying the rest, so package restore is cached between builds.
  • Never bake secrets into an image; pass them as environment variables or mounted secrets at run time.
  • Run docker compose up --build, then open http://localhost:8080.

Interview question

Why use a multi-stage Dockerfile for an ASP.NET Core app?

The SDK image needed to restore and compile is large and includes build tools. A multi-stage build compiles in an SDK stage and copies only the published output into the much smaller ASP.NET runtime image. The final image is smaller, faster to pull and has fewer tools an attacker could use.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.