Loading...
Loading...
Create optimized, secure multi-stage Dockerfiles for .NET Core and ASP.NET Core applications. Use when (1) creating a new Dockerfile for a .NET project, (2) containerizing a .NET application, (3) optimizing an existing .NET Dockerfile, (4) setting up Docker for .NET 6/7/8+ apps, or (5) user mentions .NET and Docker/container together.
npx skill4agent add jkappers/agent-skills dotnet-dockerfilesrc/MyApp/MyApp.csprojBreaking Change in .NET 10: Ubuntu is now the default Linux distribution for all .NET container images. Debian images are no longer provided. Tags like,10.0, and10.0-nobleall use Ubuntu 24.04 "Noble Numbat".10.0-noble-chiseled
| Scenario | Runtime Image | Compressed Size |
|---|---|---|
| Standard ASP.NET | | ~92 MB |
| Smaller footprint | | ~52 MB |
| Production (recommended) | | ~53 MB |
| Azure Linux distroless | | ~55 MB |
| Self-contained | | ~22 MB |
| Native AOT | | ~12 MB |
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
# Copy project files and restore (cache layer)
COPY *.csproj ./
RUN dotnet restore
# Copy source and publish
COPY . .
RUN dotnet publish -c Release -o /app/publish
# Runtime
FROM mcr.microsoft.com/dotnet/aspnet:10.0-noble-chiseled AS production
WORKDIR /app
COPY /app/publish .
USER app
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApp.dll"]FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
# Copy solution and project files (cache layer)
COPY *.sln ./
COPY src/MyApp/*.csproj ./src/MyApp/
COPY src/MyApp.Core/*.csproj ./src/MyApp.Core/
COPY tests/MyApp.Tests/*.csproj ./tests/MyApp.Tests/
RUN dotnet restore
# Copy everything and publish
COPY . .
RUN dotnet publish src/MyApp -c Release -o /app/publish
FROM mcr.microsoft.com/dotnet/aspnet:10.0-noble-chiseled AS production
WORKDIR /app
COPY /app/publish .
USER app
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApp.dll"]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 \
--self-contained true \
-r linux-x64
FROM mcr.microsoft.com/dotnet/runtime-deps:10.0-noble-chiseled AS production
WORKDIR /app
COPY /app/publish .
USER app
EXPOSE 8080
ENTRYPOINT ["./MyApp"]FROM mcr.microsoft.com/dotnet/sdk:10.0-aot AS build
WORKDIR /src
COPY *.csproj ./
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app/publish
FROM mcr.microsoft.com/dotnet/runtime-deps:10.0-noble-chiseled AS production
WORKDIR /app
COPY /app/publish .
USER app
EXPOSE 8080
ENTRYPOINT ["./MyApp"]mcr.microsoft.com/dotnet/sdk:10.0-aot10.0-noble-aotmcr.microsoft.com/dotnet/sdk:10.0-alpine-aotmcr.microsoft.com/dotnet/sdk:10.0-azurelinux3.0-aot<PropertyGroup>
<PublishAot>true</PublishAot>
</PropertyGroup># Publish to local container runtime
dotnet publish --os linux --arch x64 /t:PublishContainer
# Publish directly to a registry (no Docker needed)
dotnet publish --os linux --arch x64 /t:PublishContainer \
-p ContainerRegistry=ghcr.io \
-p ContainerRepository=myorg/myapp
# Multi-architecture publishing
dotnet publish /t:PublishContainer \
-p ContainerRuntimeIdentifiers="linux-x64;linux-arm64"<PropertyGroup>
<!-- Control image format: Docker or OCI -->
<ContainerImageFormat>OCI</ContainerImageFormat>
<!-- Customize base image -->
<ContainerBaseImage>mcr.microsoft.com/dotnet/aspnet:10.0-noble-chiseled</ContainerBaseImage>
<!-- Set image name and tag -->
<ContainerRepository>myapp</ContainerRepository>
<ContainerImageTag>1.0.0</ContainerImageTag>
</PropertyGroup>FROM mcr.microsoft.com/dotnet/sdk:10.0 AS base
WORKDIR /src
COPY *.csproj ./
RUN dotnet restore
FROM base AS development
COPY . .
ENTRYPOINT ["dotnet", "watch", "run"]
FROM base AS build
COPY . .
RUN dotnet publish -c Release -o /app/publish
FROM mcr.microsoft.com/dotnet/aspnet:10.0-noble-chiseled AS production
WORKDIR /app
COPY /app/publish .
USER app
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApp.dll"]docker build --target development -t myapp:dev .RUN \
dotnet restoreFROM mcr.microsoft.com/dotnet/sdk:10.0@sha256:abc123... AS builddocker pull mcr.microsoft.com/dotnet/sdk:10.0
docker inspect --format='{{index .RepoDigests 0}}' mcr.microsoft.com/dotnet/sdk:10.0.dockerignore**/bin/
**/obj/
**/.vs/
**/.vscode/
**/node_modules/
**/*.user
**/*.userosscache
**/*.suo
*.md
.git
.gitignore
Dockerfile*
docker-compose*USER app&&-r linux-x64