Running .NET Core Applications in a Docker Container

This document provides detailed instructions on how to build and run your .NET Core applications within Docker containers. Containerization with Docker offers a consistent and isolated environment for your applications, simplifying deployment and management.

Prerequisites

Step 1: Create a Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. For a typical .NET Core application, you'll need a Dockerfile in the root directory of your project.

Here's a common example of a Dockerfile for a .NET Core web application:

# Use an official .NET Core runtime as a parent image
            FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
            WORKDIR /app

            # Copy the C# project.assets.json file and restore dependencies
            COPY *.csproj ./
            RUN dotnet restore

            # Copy the rest of the application's source code
            COPY . .

            # Publish the application
            RUN dotnet publish -c Release -o out

            # Use a smaller runtime image for the final application
            FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
            WORKDIR /app
            COPY --from=build /app/out .

            # Expose the port the application runs on
            EXPOSE 8080

            # Define the entry point for the container
            ENTRYPOINT ["dotnet", "YourAppName.dll"]
            

Note: Replace YourAppName.dll with the actual name of your application's executable DLL.

Step 2: Build the Docker Image

Navigate to your project directory in the terminal and run the following command to build your Docker image:

docker build -t my-dotnet-app .
            

This command:

Step 3: Run the Docker Container

Once the image is built, you can run a container from it using the following command:

docker run -p 8080:80 my-dotnet-app
            

This command:

Tip: Running in Detached Mode

To run the container in the background, use the -d flag:

docker run -d -p 8080:80 my-dotnet-app
                

Step 4: Access Your Application

Open your web browser and navigate to http://localhost:8080 (or the port you mapped) to access your .NET Core application running inside the Docker container.

Common Issues and Troubleshooting

Port Conflicts

If port 8080 is already in use on your host machine, you'll encounter an error. You can either stop the process using that port or map to a different host port:

docker run -p 8081:80 my-dotnet-app
            

Application Not Starting

If the container exits immediately after starting, check the container logs for errors:

docker logs 
            

You can find the container_id by running docker ps -a.

Incorrect Dockerfile Configuration

Ensure your Dockerfile correctly copies your application files and specifies the correct entry point. The use of multi-stage builds (as shown in the example) helps in creating smaller, more secure final images.

Note on Base Images

Microsoft provides optimized base images for .NET Core. For production, consider using runtime-only images (like mcr.microsoft.com/dotnet/aspnet:8.0) to reduce image size and attack surface.