How to Dockerize a .NET 8 ASP.NET Core Web Application
In this guide, we will walk through the steps to set up Docker and .NET 8 SDK on an Ubuntu server, create a simple ASP.NET Core web application, and run it inside a Docker container.
Step 1: Installing Docker
First, install Docker on your system. Run the following commands:
sudo apt-get update -y
sudo apt-get install ca-certificates curl -y
sudo install -m 0755 -d /etc/apt/keyrings -y
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
Next, add the Docker repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Finally, update and install Docker:
sudo apt-get update -y
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
Step 2: Installing Required Dependencies
To resolve the issue related to libicu
, install the necessary packages:
sudo apt-get update
sudo apt-get install -y libicu-dev apt-transport-https
Step 3: Installing Microsoft Package Repository and .NET 8 SDK
Add the Microsoft package repository:
wget https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
Now, install the .NET 8 SDK:
sudo apt-get update
sudo apt-get install -y dotnet-sdk-8.0
Step 4: Verifying the Installation
You can verify the installation of .NET by checking the version:
dotnet --version
Step 5: Creating an ASP.NET Core Web Application
After installing the .NET 8 SDK, you can create a new ASP.NET Core web application:
mkdir AspNetCoreWebApp
cd AspNetCoreWebApp
dotnet new web -o MyWebApp
Step 6: Creating a Dockerfile
Navigate to the MyWebApp
directory and create a Dockerfile:
cd MyWebApp
touch Dockerfile
In the Dockerfile
, add the following contents:
dockerfile
# Use .NET 8 SDK to build the application
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
# Set the working directory
WORKDIR /app
# Copy the project files
COPY . .
# Restore and build the project
RUN dotnet restore
RUN dotnet publish -c Release -o out
# Final stage: run the application using the ASP.NET Core runtime
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app/out .
# Expose port 80 for the application
EXPOSE 80
# Set the application to listen on IPv4 only
ENV ASPNETCORE_URLS=http://0.0.0.0:80
# Run the application
ENTRYPOINT ["dotnet", "MyWebApp.dll"]
Step 7: Building the Docker Image
Once the Dockerfile
is ready, build the Docker image using the following command:
docker build -t my-dotnet8-webapp .
Step 8: Running the Docker Container
After building the image, you can run the Docker container using:
docker run -d -p 8080:80 --name dotnet8_web_sample my-dotnet8-webapp
This command will make your web application accessible at http://Server-IP:8080
.
By following these steps, you now have a .NET 8 ASP.NET Core web application running inside a Docker container. This setup allows for easy deployment and scalability.