Introduction
In this blog, we learn how to build and containerize an ASP.NET core app.
Prerequisites
Install Visual Studio from https://visualstudio.microsoft.com/. During installation, select ASP.NET and web development workload.
Install Docker Desktop and ensure it is running.
Overview/Workflow
Here is the complete sequence of steps that we follow.
Steps to create asp.net core application:
Step 1: Create a project
Open Visual Studio, on the start window, select Create a new project
Step 2: In the "Create a new project" window, select C# from the Language dropdown, Windows from the Platform dropdown, and Web from the Project Types dropdown. After applying these filters, choose the ASP.NET Core Web App template, then click Next.
Step 3: In the Configure your new project window, enter myfirstaspwebapp in the Project name field. Then, select Next.
Step 4: In the Additional information window, verify that .NET 8.0 appears in the Target Framework field.
Step 5: Select CREATE
Visual Studio opens your new project.
Step 6: Build the solution
When you build a solution in Visual Studio, it performs an incremental compilation, converting the source code into executable code.
Step 7: Clean Solution
When you clean a solution in Visual Studio, it removes all intermediate and output files, including compiled assemblies and object files, allowing for a complete project rebuild.
Steps to containerize your asp.net project:
Step 1: Add a Dockerfile
A Dockerfile is a text file containing the commands needed to assemble a Docker image.
In the root directory of your project, create a Dockerfile with the instructions Docker will use to build your application image.
For ASP.NET Core applications, a basic Dockerfile might look like this:
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /source
# copy csproj and restore as distinct layers
COPY *.sln .
COPY myfirstaspwebapp/*.csproj ./myfirstaspwebapp/
RUN dotnet restore
# copy everything else and build app
COPY myfirstaspwebapp/. ./myfirstaspwebapp/
WORKDIR /source/myfirstaspwebapp
RUN dotnet publish -c release -o /app --no-restore
# final stage/image
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "myfirstaspwebapp.dll"]
Overview of the line of code used in the Dockerfile.
Command | Description |
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build | Uses the official .NET 8.0 SDK image as the base for the build stage, including compilers and CLI tools. |
WORKDIR /source | Sets the working directory inside the container to /source. |
COPY *.sln . | Copies the solution (.sln) file from the host to the /source directory in the container. |
COPY aspnetapp/*.csproj ./aspnetapp/ | Copies the application's .csproj file to /source/aspnetapp/ in the container. |
RUN dotnet restore | Restores all NuGet dependencies based on the solution and project files, caching them for faster future builds. |
COPY aspnetapp/. ./aspnetapp/ | Copies the entire source code of the application from the host to /source/aspnetapp/. |
WORKDIR /source/aspnetapp | Changes the working directory to the app folder (/source/aspnetapp) inside the container. |
RUN dotnet publish -c release -o /app --no-restore | Builds the application in "release" mode, placing the output in /app. Skips restoring dependencies again (--no-restore). |
COPY --from=build /app ./ | Copies the compiled application files from the /app directory of the build stage into the final image. |
ENTRYPOINT ["dotnet", "aspnetapp.dll"] | Specifies the command to run the application, using dotnet to start the aspnetapp.dll file when the container starts. |
Step 2: Build the Docker Image
A Docker image is a standardized package containing all the necessary files, binaries, libraries, and configurations to run a container.
In a terminal or PowerShell, navigate to your project directory and run the following command to build your Docker image:
docker build -t myfirstaspwebapp .
· Replace myfirstaspwebapp with a meaningful name for your image.
· The . at the end specifies the current directory as the build context.
Step 3: Run the Docker Image
After building the image, you can run it using the following command:
docker run -it --rm -p 83:8080 --name myfirstaspwebapp_sample myfirstaspwebapp
Allocate a pseudo-TTY and keep it open, equivalent to using --interactive --tty.
Automatically remove the container upon exit.
Map <port> on the host machine to port 8080 in the container. [in this case I have selected port 83]
Name the container myfirstaspwebapp_sample .
Use the myfirstaspwebapp image.
Now, your application should be accessible on Home page - myfirstaspwebapp
Step 4: View Running Containers
A container is a portable, self-contained package with everything required to run a software application or service.
To list running Docker containers, use:
docker ps
You can also check your docker engine to see if the container is running:
Additional Steps: -
Stop and Remove Containers
To stop a running container:
docker stop your-image-name
To remove a stopped container:
docker rm
References: -
Project Repository:
Ücretsiz rastgele yetişkin sohbet cinsel sohbet gabile sohbet sorunsuz yeni kişilerle tanışma imkanı sağlar.
Ücretsiz rastgele yetişkin chat gabile chat cinsel chat kesintisiz arkadaşlık yeni kişilerle tanışma imkanı sağlar.