Docker - Setting ASP.Net
Docker - Setting up ASP.NET Core
Section titled “Docker - Setting up ASP.NET Core”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.
Prerequisites
Section titled “Prerequisites”- 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).
Creating a Sample ASP.NET Core App
Section titled “Creating a Sample ASP.NET Core App”Step 1: Create a new ASP.NET Core web application using the .NET CLI.
# Create a new directory and navigate into itmkdir my-aspnet-appcd my-aspnet-app
# Create a new minimal web API project (or use 'webapp' for Razor Pages/MVC)dotnet new webapiThis creates a basic project structure including .csproj, Program.cs, etc.
Creating the Dockerfile
Section titled “Creating the Dockerfile”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 frameworkFROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-envWORKDIR /app
# Copy csproj and restore as distinct layers to leverage Docker cacheCOPY *.csproj .RUN dotnet restore
# Copy the rest of the application codeCOPY . .
# Publish the application# Use Release configuration and output to /app/outRUN dotnet publish -c Release -o out --no-restore
# Build runtime image# Use the official ASP.NET Core runtime image, which is smaller than the SDKFROM mcr.microsoft.com/dotnet/aspnet:7.0WORKDIR /app
# Copy the published output from the build stageCOPY --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 fileENTRYPOINT ["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 SDKimage, which contains tools needed to build and publish the application. It restores dependencies, copies code, and publishes the release build to an/app/outdirectory. - Stage 2 (Final Stage): Starts from the much smaller
ASP.NET Runtimeimage, which only contains dependencies needed to run the application. It copies only the published application artifacts from thebuild-envstage (COPY --from=build-env ...). This results in a significantly smaller final image. ENTRYPOINT: Configures the container to run the application usingdotnet YourApp.dll.
Building the Docker Image
Section titled “Building the Docker Image”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 .Running the Container
Section titled “Running the Container”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 80docker run -d -p 5000:80 --name my-running-app my-aspnet-app:1.0Step 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.