Skip to content

Docker - Setting ASP.Net

ASP.NET Core is Microsoft’s modern, cross-platform, open-source framework for building web applications. Docker provides an excellent way to package and deploy ASP.NET Core applications consistently across different environments.

This chapter focuses on containerizing a basic ASP.NET Core application using official Microsoft Docker images.

  • Docker installed (Docker Desktop on Windows/Mac, or Docker Engine on Linux).
  • .NET SDK installed locally (to create the initial project, though not strictly required if you only build using Docker).

Step 1: Create a new ASP.NET Core web application using the .NET CLI.

# Create a new directory and navigate into it
mkdir my-aspnet-app
cd my-aspnet-app
# Create a new minimal web API project (or use 'webapp' for Razor Pages/MVC)
dotnet new webapi

This creates a basic project structure including .csproj, Program.cs, etc.

Step 2: Create a file named Dockerfile in the my-aspnet-app directory. A common pattern for .NET applications is a multi-stage build, which optimizes the final image size.

# Use the official .NET SDK image for building the application
# Choose a specific version matching your project's target framework
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers to leverage Docker cache
COPY *.csproj .
RUN dotnet restore
# Copy the rest of the application code
COPY . .
# Publish the application
# Use Release configuration and output to /app/out
RUN dotnet publish -c Release -o out --no-restore
# Build runtime image
# Use the official ASP.NET Core runtime image, which is smaller than the SDK
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
# Copy the published output from the build stage
COPY --from=build-env /app/out .
# Expose the port the application listens on (default is 80 for Kestrel in container)
EXPOSE 80
# Define the entry point for the container
# Replace 'MyWebApp.dll' with the actual name of your application's DLL file
ENTRYPOINT ["dotnet", "my-aspnet-app.dll"]

Important: Replace my-aspnet-app.dll in the ENTRYPOINT instruction with the actual name of the DLL file generated for your project (usually found in the bin/Release/netX.X/ folder after publishing, and matching your .csproj file name).

Explanation of the multi-stage build:

  • Stage 1 (build-env): Uses the larger .NET SDK image, which contains tools needed to build and publish the application. It restores dependencies, copies code, and publishes the release build to an /app/out directory.
  • Stage 2 (Final Stage): Starts from the much smaller ASP.NET Runtime image, which only contains dependencies needed to run the application. It copies only the published application artifacts from the build-env stage (COPY --from=build-env ...). This results in a significantly smaller final image.
  • ENTRYPOINT: Configures the container to run the application using dotnet YourApp.dll.

Step 3: Build the image using docker build. Make sure you are in the my-aspnet-app directory.

docker build -t my-aspnet-app:1.0 .

Step 4: Run the container from your newly built image, mapping a host port to the container’s exposed port (80).

# Run the container, mapping host port 5000 to container port 80
docker run -d -p 5000:80 --name my-running-app my-aspnet-app:1.0

Step 5: Test the application. Open your browser and navigate to http://localhost:5000. If you used the webapi template, try accessing http://localhost:5000/weatherforecast.

You have now successfully containerized and run an ASP.NET Core application using Docker.