Upgrading a .NET application to a newer SDK often brings useful improvements to development, compilation, and application performance. However, developers sometimes notice that their Docker images become larger after updating the SDK image.
A common reason is that the .NET SDK image contains much more than the application needs at runtime. It includes build tools, compilers, command-line utilities, reference assemblies, and other development components.
These tools are necessary when restoring packages, compiling code, and publishing an application. They are generally not required when the application is running in production.
The problem occurs when a Dockerfile uses the SDK image as the final application image. The resulting container carries development components that are unnecessary for serving requests.
The solution is not to avoid the newer SDK. Instead, separate the build environment from the runtime environment, copy only the published application into the final image, and select a runtime image that matches the application's requirements.
This article explains how to keep Docker images small when using the .NET 11 SDK, while maintaining a reliable build and deployment process.
Why the .NET SDK Image Is Larger
A .NET SDK container is designed for development and application publishing. It includes several components:
- .NET command-line tools.
- Compilers and build utilities.
- NuGet package management support.
- .NET runtime components.
- ASP.NET Core components.
- Reference assemblies and other development dependencies.
Consider this Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:11.0
WORKDIR /app
COPY . .
RUN dotnet publish -c Release
ENTRYPOINT ["dotnet", "MyApplication.dll"]
This Dockerfile builds and runs the application using the same image.
Although the application works, the final container still contains the SDK and its development tools.
For a production application, these components increase the image size and introduce unnecessary software into the runtime environment.
A better approach is to use separate Docker build stages.
Use Multi-Stage Builds
Docker multi-stage builds allow developers to use one image for building an application and another image for running it.
The SDK image performs the compilation and publishing work. The final stage uses a smaller runtime image and copies only the published output.
For an ASP.NET Core application, the Dockerfile can look like this:
# Build stage
FROM mcr.microsoft.com/dotnet/sdk:11.0 AS build
WORKDIR /source
COPY MyApplication.csproj ./
RUN dotnet restore MyApplication.csproj
COPY . .
RUN dotnet publish MyApplication.csproj \
-c Release \
-o /app/publish \
--no-restore
# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:11.0 AS final
WORKDIR /app
COPY --from=build /app/publish .
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApplication.dll"]
There are two stages in this example.
The build stage uses the .NET 11 SDK image to restore dependencies and publish the application.
The final stage uses the ASP.NET Core runtime image. It receives only the published files from the build stage.
The SDK and build tools are not copied into the final image.
This is one of the most effective ways to reduce the size of a production .NET container.
For an application that does not use ASP.NET Core, use the corresponding mcr.microsoft.com/dotnet/runtime:11.0 image instead of the ASP.NET Core image.
Improve Docker Layer Caching
Reducing image size is important, but build performance also matters.
A poorly structured Dockerfile may restore NuGet packages every time a source file changes. This increases build time and creates unnecessary work in CI/CD pipelines.
Consider this structure:
FROM mcr.microsoft.com/dotnet/sdk:11.0 AS build
WORKDIR /source
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o /app/publish
The problem is that the entire source directory is copied before package restoration.
When a developer changes a single C# file, Docker may invalidate the layer containing the restore operation. The build then restores packages again.
A better approach is to copy project files first.
FROM mcr.microsoft.com/dotnet/sdk:11.0 AS build
WORKDIR /source
COPY MyApplication.csproj ./
RUN dotnet restore
COPY . .
RUN dotnet publish \
-c Release \
-o /app/publish \
--no-restore
Now Docker can reuse the restore layer when only application source files change.
For projects with multiple dependencies, copy all relevant project files before running restore. This includes project files referenced by the main application and any solution-level dependency configuration required for restoration.
The --no-restore option tells the publish command to use the packages already restored in the earlier stage.
This approach improves build caching without adding unnecessary files to the final image.
Use BuildKit Cache Mounts for NuGet Packages
Docker BuildKit provides cache mounts that can preserve package downloads between builds.
This is useful when CI/CD pipelines repeatedly build the same application.
For example:
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:11.0 AS build
WORKDIR /source
COPY MyApplication.csproj ./
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet restore MyApplication.csproj
COPY . .
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish MyApplication.csproj \
-c Release \
-o /app/publish \
--no-restore
FROM mcr.microsoft.com/dotnet/aspnet:11.0 AS final
WORKDIR /app
COPY --from=build /app/publish .
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApplication.dll"]
The cache mount stores NuGet packages outside the normal image layers.
This allows subsequent builds to reuse downloaded packages instead of retrieving them again.
The cache is used during the build process and is not copied into the final runtime image.
BuildKit cache mounts are especially useful in environments where the Docker builder retains its cache between builds. CI/CD systems that create a fresh builder for every run may need additional cache configuration to get the same benefit.
Add a Proper .dockerignore File
Another common reason for large Docker build contexts is copying unnecessary files into the build environment.
For example, a developer's project directory may contain:
- bin and obj folders.
- Local build outputs.
- Git repository data.
- IDE configuration files.
- Test results.
- Temporary files.
- Local environment configuration.
These files are usually not required to compile the application inside Docker.
Create a .dockerignore file in the project root:
bin/
obj/
.git/
.vs/
.vscode/
TestResults/
*.user
*.suo
*.log
.env
This prevents Docker from sending matching files into the build context.
The .dockerignore file does not directly remove files from an existing image. Instead, it prevents unnecessary files from entering the build process through operations such as COPY . ..
Keep required configuration files and project dependencies available to the build. Do not exclude files that are needed for compilation, publishing, or application startup.
Choose the Right .NET Runtime Image
Not every .NET application requires the same runtime image.
Microsoft provides different container image repositories for different application types.
| Image |
Intended use |
| dotnet/sdk |
Building, testing, and publishing applications |
| dotnet/aspnet |
Running ASP.NET Core applications |
| dotnet/runtime |
Running non-ASP.NET Core .NET applications |
| dotnet/runtime-deps |
Providing native runtime dependencies for suitable self-contained or Native AOT applications |
For an ASP.NET Core API, the ASP.NET Core runtime image is normally the appropriate choice.
For a console application or worker service, the .NET runtime image may be sufficient.
Using an SDK image as the final stage should generally be avoided unless the container genuinely needs SDK tools at runtime.
Also pay attention to the operating system variants of the images. .NET 11 container tags are available for different Linux distributions, including Ubuntu, Alpine, and Azure Linux.
When selecting a variant, consider native dependencies, globalization requirements, diagnostics tools, and compatibility with the application's libraries.
Matching the SDK and runtime operating system variants can also make the build environment more predictable.
Consider Chiseled and Distroless Images
For applications with strict image-size and security requirements, smaller runtime image variants may be worth considering.
Microsoft provides chiseled and distroless .NET images.
These images remove many operating system components that are not required to run the application.
Potential benefits include:
- Smaller deployment images.
- Fewer unnecessary operating system packages.
- Reduced attack surface.
- Less software to maintain inside the container.
However, minimal images also have limitations.
Some variants do not include a shell or package manager. This means developers cannot rely on commands such as apt-get, bash, or other diagnostic utilities inside the running container.
Globalization and time-zone support also require attention. An application that depends on culture-specific formatting may need an image variant with the appropriate ICU libraries and globalization data.
Before adopting a chiseled or distroless image, test the application with realistic production data and verify its native dependencies.
A smaller image is useful only when the application continues to work correctly.
Avoid Installing Development Tools in the Runtime Stage
A common Dockerfile mistake is installing additional development packages in the final stage.
For example:
FROM mcr.microsoft.com/dotnet/aspnet:11.0
RUN apt-get update && \
apt-get install -y curl git build-essential
These tools may be useful during development, but they should not automatically become part of the production image.
If a package is required only for compilation, install it in the build stage.
If a native library is required by the application at runtime, make sure it is available in the final image.
The distinction is important: removing unnecessary development tools is good, but removing required runtime dependencies can cause application failures.
Measure the Final Image Instead of Guessing
After changing the Dockerfile, compare the resulting images.
Build the application:
docker build -t myapp:optimized .
Inspect the image size:
docker images myapp
To inspect the image layers, use:
docker history myapp:optimized
For a more detailed investigation, Docker Buildx also provides build inspection and image analysis capabilities.
Compare the previous and updated images using the same architecture, runtime variant, and build configuration.
Do not assume that switching to a smaller base image will always produce the expected result. Published application files, native dependencies, globalization data, and application content all contribute to the final size.
Common Mistakes to Avoid
| Mistake |
Why it creates problems |
Better approach |
| Using the SDK image as the final stage |
Includes unnecessary development tools |
Use a separate runtime stage |
| Copying the entire source directory before restore |
Reduces Docker cache reuse |
Copy project files first |
| Ignoring .dockerignore |
Sends unnecessary files into the build context |
Exclude local build and development files |
| Choosing the wrong runtime image |
May include unnecessary components or miss required frameworks |
Match the image to the application type |
| Installing build tools in the runtime stage |
Increases image size and software exposure |
Install build dependencies only in the build stage |
| Choosing an extremely minimal image without testing |
May remove required native or diagnostic components |
Validate runtime dependencies before deployment |
| Comparing different image variants |
Produces misleading size comparisons |
Compare equivalent operating systems and architectures |
Best Practices for Production Applications
- Use the .NET 11 SDK image only in the build stage.
- Use the ASP.NET Core runtime image for web applications and the .NET runtime image for suitable non-web applications.
- Copy project files before restoring packages to improve Docker layer caching.
- Use BuildKit cache mounts when repeated package downloads slow down CI/CD builds.
- Maintain a .dockerignore file to prevent local development files from entering the build context.
- Select runtime image variants based on application requirements rather than image size alone.
- Run the application as a non-root user and follow the security recommendations for the selected base image.
- Measure image size and inspect layers after changing the Dockerfile.
- Rebuild and scan images regularly so that base-image security updates are included in deployments.
- Test the final container with the same environment variables, ports, native dependencies, and configuration used in production.
Summary
Adding the .NET 11 SDK to a Docker-based application does not mean the production container must become larger.
The main improvement is to separate building and running into different Docker stages. Use the SDK image to restore packages and publish the application, then copy only the published output into an appropriate runtime image.
Improve the process further with Docker layer caching, BuildKit cache mounts, and a well-maintained .dockerignore file. For applications with stricter size and security requirements, consider chiseled or distroless runtime images after validating their compatibility.
The important principle is simple: production containers should contain what the application needs to run, not everything required to build it.
Join the conversation! Your thoughts help the community grow.