Introduction
In the previous lesson, we ran the ASP.NET Core Web API in a basic container. Although that approach starts the application successfully, it may place development tools and runtime files in the same image. As a result, the image can be larger than necessary and may contain the .NET SDK, compilers, source code, and other components that are not required when the API is running in production.
In this lesson, we will create a cleaner production-oriented structure by using a multi-stage build. We will define separate restore, build, publish, and runtime stages in one Dockerfile. The compilation work will run in a .NET SDK image, while the final image will contain only the ASP.NET Core runtime and the published application files.
Understanding SDK and runtime images
The .NET container image family provides images for different purposes. The mcr.microsoft.com/dotnet/sdk image contains the .NET CLI, compiler, NuGet tooling, and publishing tools. It is appropriate for restore, build, test, and publish operations. However, those development tools are not required by a running API.
The mcr.microsoft.com/dotnet/aspnet image is designed to run ASP.NET Core applications. It does not provide the SDK tools required to compile the application. Instead, it focuses on the runtime components needed in a production environment. Separating these roles helps reduce the final image size and avoids shipping unnecessary tools.
| Image | Purpose | Typical stage |
|---|---|---|
| dotnet/sdk | Restore, build, test, and publish operations | Build environment |
| dotnet/aspnet | Running an ASP.NET Core application | Runtime environment |
A multi-stage build allows us to copy only the artifacts produced by earlier stages into the final stage. The compiler, NuGet cache, and source files remain in the SDK-based stages and are not copied into the runtime image.
Preparing the project structure
We will assume that the repository has the following structure. If your project has a different name, update the paths and DLL name in the Dockerfile accordingly.
Dockerfile .dockerignore src/ Api/ Api.csproj Program.cs Controllers/
The Dockerfile should be executed from the repository root. This matters because the source paths used by COPY are evaluated relative to the Docker build context.
Creating the .dockerignore file
Create a .dockerignore file in the repository root to prevent unnecessary files from being sent to Docker during the build. The file is not directly included in the image; it controls which files are transferred from the host into the build context.
**/bin **/obj .git .gitignore .vs .vscode *.user *.suo Dockerfile* README.md
Excluding the bin and obj directories is especially important. These directories contain local build output that can vary by operating system and SDK version. Because the files can be recreated inside the container, sending them as part of the build context is unnecessary.
Creating the multi-stage Dockerfile
We will create a Dockerfile with four stages:
- restore: Downloads project dependencies.
- build: Compiles the application using the Release configuration.
- publish: Produces the deployment-ready output.
- final: Copies the published files into the ASP.NET Core runtime image.
Create the following Dockerfile in the repository root:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS restore WORKDIR /src COPY src/Api/Api.csproj src/Api/ RUN dotnet restore src/Api/Api.csproj FROM restore AS build COPY src/Api/ src/Api/ WORKDIR /src/Api RUN dotnet build Api.csproj \ --configuration Release \ --no-restore \ --output /app/build FROM build AS publish RUN dotnet publish Api.csproj \ --configuration Release \ --no-restore \ --output /app/publish \ /p:UseAppHost=false FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final WORKDIR /app ENV ASPNETCORE_HTTP_PORTS=8080 EXPOSE 8080 COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "Api.dll"]
The AS restore, AS build, AS publish, and AS final expressions assign names to the stages. These names allow us to copy files from a specific stage with an instruction such as COPY --from=publish.
The restore stage
In the first stage, we copy only the Api.csproj file into the image and run dotnet restore. The source code files are copied later. This arrangement allows Docker to reuse the restore layer when only application code changes.
Docker caches layers created during previous builds whenever the relevant inputs have not changed. If the project file remains unchanged, the dotnet restore layer can be reused. This can significantly reduce build time when the application uses many NuGet packages.
The build stage
The build stage is based on the restore stage. It copies the application source files into the image and compiles them. The --no-restore option prevents restore from running again because dependencies were already restored in the previous stage.
The --configuration Release option creates a release-oriented build. The --output /app/build option places the build output in a predictable directory.
The publish stage
The dotnet build command compiles the application, but deployment normally uses the output of dotnet publish. Publishing gathers the files required to run the application under /app/publish.
The /p:UseAppHost=false property is used for a framework-dependent deployment. It produces a DLL-based output rather than an operating-system-specific native executable. The final image starts the application with dotnet Api.dll.
The final runtime stage
The final stage uses mcr.microsoft.com/dotnet/aspnet:8.0 instead of the SDK image. It does not contain the compiler or source code. The published files are copied from the publish stage by using COPY --from=publish.
ASP.NET Core container images for .NET 8 can use port 8080 as the application port. Therefore, we explicitly configure ASPNETCORE_HTTP_PORTS=8080 and document the port with EXPOSE 8080. Host-to-container port mapping will still be configured separately when the container is started.
Building and running the image
From the directory containing the Dockerfile, build the image:
docker build -t api-multistage:1.0 .
The build output should show the restore, build, publish, and final stages. Inspect the resulting image with:
docker images api-multistage
Start a container with the following command:
docker run --name api-multistage-container \ --publish 8080:8080 \ api-multistage:1.0
If the application provides a health endpoint, test http://localhost:8080/health. If Swagger is enabled, you may also test http://localhost:8080/swagger. The actual endpoint paths depend on the application configuration.
Open another terminal and inspect the container logs:
docker logs api-multistage-container
When the test is complete, stop and remove the container:
docker stop api-multistage-container docker rm api-multistage-container
Observing build cache behavior
The order of file-copy instructions affects build performance. This is why the project file is copied and restored before the rest of the source code. After the first build, make a small change to Program.cs and build the image again:
docker build -t api-multistage:1.1 .
Docker should report that the restore step was taken from the cache. If Api.csproj changes, the restore step will normally run again because the dependency definitions may have changed.
Solutions containing multiple projects
Real applications often reference projects such as Application, Infrastructure, or Domain. In that situation, copying only Api.csproj is not sufficient. The restore stage must copy every referenced project file while preserving its directory structure.
COPY src/Api/Api.csproj src/Api/ COPY src/Application/Application.csproj src/Application/ COPY src/Infrastructure/Infrastructure.csproj src/Infrastructure/ COPY src/Domain/Domain.csproj src/Domain/ RUN dotnet restore src/Api/Api.csproj
After the project files are copied and restored, copy the source directories:
COPY src/Api/ src/Api/ COPY src/Application/ src/Application/ COPY src/Infrastructure/ src/Infrastructure/ COPY src/Domain/ src/Domain/
This layout allows the dependency restore layer to remain valid across more source-code-only changes.
Common mistakes
- Using the wrong build context: Running docker build from src/Api may hide the root Dockerfile or referenced projects. Run the command from the repository root.
- Confusing the project name and DLL name: The DLL in ENTRYPOINT normally matches the project file name. If the project is StoreApi.csproj, use StoreApi.dll.
- Using the SDK image for the final stage: The application may work, but the final image will be unnecessarily large.
- Using --no-restore without restoring first: Build and publish will fail if dependencies were not restored in an earlier stage.
- Confusing container and host ports: EXPOSE does not create host port mapping. You still need a command such as docker run -p 8080:8080.
- Copying local bin and obj output: These files can invalidate cache layers and create platform-specific problems. Use .dockerignore.
Short practice exercise
Complete the following exercise:
- Create a .dockerignore file appropriate for your API project.
- Write a Dockerfile with restore, build, publish, and final stages.
- Build the image with the tag api-practice:1.0.
- Run the container so that it is available on host port 8090.
- Inspect the container logs and image size.
- Make a small source-code change, rebuild the image, and identify which steps came from the cache.
docker build -t api-practice:1.0 . docker run --name api-practice-container -p 8090:8080 api-practice:1.0 docker images api-practice docker logs api-practice-container
Conclusion
In this lesson, we created a four-stage multi-stage Dockerfile for an ASP.NET Core API. We restored dependencies in the restore stage, compiled the application in the build stage, generated deployment output in the publish stage, and copied only the required files into the ASP.NET Core runtime image.
The SDK image should be used for development and compilation, while the ASP.NET Core runtime image should be used for the running application. This separation reduces image size, makes the build process more predictable, and prevents unnecessary tools from being shipped in the production image. In the next lessons, we will improve container configuration with environment variables and prepare the application to communicate with a SQL Server container.