Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy ASP.NET Core programs using Docker

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Docker to deploy ASP.NET Core programs, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

I. Foreword

This article describes how to deploy an ASP.NET Core application in Docker. The development tools are Visual Studio 2019 and VS Code.

Second, use Docker deployment

We chose to create a new MVC project for demonstration.

1. Create a new MVC project

Template Select ASP.NET Core Web Application, as shown below:

Enter the project name and click "Create":

Select the Web application (Model View Controller) and check "Enable Docker Support" as shown below:

Then check if Docker environment is installed, as shown below:

An MVC project is created and a Dockerfile is automatically generated:

If "Enable Docker Support" is not checked when creating the project, we can also add the Dockerfile file after the project is created, right click on the project, and select "Docker Support" below the "Add" option, as shown in the following figure:

Then select Linux:

You can also add Dockerfile files.

The easiest way to create a Dockerfile is to create a new txt file and rename it Dockerfile.

2. Write Dockerfile

Let's look at the auto-generated Dockerfile file:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS baseWORKDIR /appEXPOSE 80FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS buildWORKDIR /srcCOPY ["DockerDemo/DockerDemo.csproj", "DockerDemo/"]RUN dotnet restore "DockerDemo/DockerDemo.csproj"COPY . .WORKDIR "/src/DockerDemo"RUN dotnet build "DockerDemo.csproj" -c Release -o /app/buildFROM build AS publishRUN dotnet publish "DockerDemo.csproj" -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY --from=publish /app/publish .ENTRYPOINT ["dotnet", "DockerDemo.dll"]

You can see that there are build and publish commands here. The automatically generated Dockerfile may have some errors, so we write a Dockerfile ourselves. The modified Dockerfile is as follows:

#Use runtime mirror FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim# to set up working directory WORKDIR /app#Copy the contents of directory to current directory COPY . .# Expose port 80 EXPOSE 80#Run mirror entry command and executable file name ENTRYPOINT ["dotnet", "DockerDemo.dll"]

Modify Dockerfile attributes to Always Copy:

This ensures that Dockerfiles can be distributed with the program.

3. Create a mirror image

First publish the program. After the program is successfully published, enter the publication directory and you can generate an image. PowerShell is used here. Use the following command to generate a mirror:

docker build -t aspnetcoredocker .

where aspnetcoredocker is the mirror name.

The last English state of the command above. It cannot be omitted.

As shown below:

If all five steps shown in the above figure are successful, it means that the mirror generation is successful. Let's look at the local mirror:

You can see, there are already mirrors that we created. We can also use VS Code to view it. VS Code requires a plugin to be installed:

After the installation is complete, you can see the docker icon on the left:

Using this plugin, you can see all the current local mirrors:

4. Run Mirror

After the above image is successfully built, we can use the following command to run the image:

docker run --name=aspnetcoredocker -p 6666:80 -d aspnetcoredocker

--name: Specifies the container name.

--p: Specifies the container port.

--d: Specifies that the container runs in the background.

As shown below:

The following long string appears to indicate that the mirroring was successful. You can view running containers using the following command:

docker ps

As shown below:

You can see that the mirror you just created is already running. If you want to see it more intuitively, you can see it in the VS Code plugin:

Green triangles represent containers that are running, and red boxes represent containers that are stopped.

5. Verification

The container runs successfully. We visit it in the browser:

A simple sample program is completed.

About "how to use Docker to deploy ASP.NET Core program" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report