The Problem
Getting a .NET application from your local machine to a production server shouldn't require a PhD in DevOps. Yet many teams spend days configuring build pipelines, wrestling with Dockerfile best practices, and debugging container networking issues that only appear in production.
Common Docker mistakes for .NET applications include: using the full SDK image in production (bloating your image by 500MB+), not leveraging multi-stage builds, copying unnecessary files into the container, running as root, and failing to configure health checks. Each of these mistakes increases your attack surface, deployment time, and hosting costs.
You need a Docker configuration that produces minimal, secure, production-ready images with health checks, proper user permissions, and optimized layer caching for fast CI/CD builds.
Ingredients
Docker Desktop
vlatest
Container runtime for building and running images
.NET 10 SDK
v10.0
.NET 10 SDK for building the application
PostgreSQL
v17
PostgreSQL database running in Docker
Redis
v7
Redis cache running in Docker
This recipe uses Docker with multi-stage builds, the official .NET 10 images, and a docker-compose configuration for local development with PostgreSQL and Redis. The Dockerfile is optimized for layer caching, security, and minimal image size.
No special NuGet packages are required beyond what your application already uses \u2014 Docker wraps your existing application without code changes.
Preparation
Ensure Docker Desktop (or Docker Engine on Linux) is installed on your machine. You'll also need a .dockerignore file to prevent unnecessary files from being copied into the build context:
**/bin/
**/obj/
**/node_modules/
**/.git/
**/.vs/
**/Dockerfile*
**/docker-compose*
**/*.md
**/.env
This .dockerignore reduces your build context size dramatically, speeding up builds by preventing Docker from sending gigabytes of unnecessary files to the build daemon.
Structure your solution so the Dockerfile sits at the solution root, next to your .sln file. This gives Docker access to all projects in the solution during the build stage while keeping the final image minimal.
The Code
Here's a production-optimized Dockerfile using multi-stage builds, and a docker-compose configuration for local development. The Dockerfile leverages layer caching to minimize rebuild times during development while producing a minimal runtime image for production.
# Stage 1: Restore dependencies (cached layer)
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS restore
WORKDIR /src
# Copy only project files first for better layer caching
COPY src/MyApp.Web/MyApp.Web.csproj src/MyApp.Web/
RUN dotnet restore src/MyApp.Web/MyApp.Web.csproj
# Stage 2: Build and publish
FROM restore AS build
WORKDIR /src
COPY src/ src/
RUN dotnet publish src/MyApp.Web/MyApp.Web.csproj \
-c Release \
-o /app/publish \
--no-restore \
/p:UseAppHost=false
# Stage 3: Production runtime (minimal image)
FROM mcr.microsoft.com/dotnet/aspnet:10.0-alpine AS runtime
WORKDIR /app
# Create non-root user for security
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# Install curl for health checks
RUN apk add --no-cache curl
# Copy published output
COPY --from=build /app/publish .
# Set environment
ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production
ENV DOTNET_EnableDiagnostics=0
# Switch to non-root user
USER appuser
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
ENTRYPOINT ["dotnet", "MyApp.Web.dll"]
version: '3.8'
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- ConnectionStrings__DefaultConnection=Host=db;Port=5432;Database=myapp;Username=postgres;Password=postgres
- Redis__ConnectionString=redis:6379
- ASPNETCORE_ENVIRONMENT=Development
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
db:
image: postgres:17-alpine
environment:
POSTGRES_DB: myapp
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redisdata:/data
volumes:
pgdata:
redisdata:
Plating
With this Docker configuration, you get a production-ready deployment pipeline that produces images under 100MB, runs as a non-root user, includes health checks, and leverages layer caching for fast rebuilds.
The multi-stage build ensures that your production image contains only the compiled application and the .NET runtime \u2014 no SDK, no source code, no build artifacts. The Alpine base image further reduces the attack surface and image size.
The docker-compose configuration gives your team a one-command development environment: docker compose up and you have a fully functional application with PostgreSQL and Redis, ready for development. When it's time to deploy, the same Dockerfile produces an optimized production image that's battle-tested because it's the same build process you've been using in development all along.
Was this recipe helpful?
Chef George
.NET Architect & Head Chef
25+ years crafting enterprise C# solutions. Creator of the Noundry platform. Passionate about clean architecture, cloud-agnostic deployments, and code that cooks.