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 run ASP.NET Core 2.1 in Docker

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about how to run ASP.NET Core 2.1in Docker. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Create a new ASP.NET Core 2.1project

Then run the project to make sure that the project we just set up works properly.

two。 Write Dockerfile

Create a new text file named Dockerfile

FROM microsoft/dotnet:2.1-aspnetcore-runtimeWORKDIR / appCOPY. .EXPOSE 80ENTRYPOINT ["dotnet", "AspNetCore.Docker.dll"]

Here we need to use the official image: microsoft/dotnet:2.1-aspnetcore-runtime

If you want to try a smaller image, you can try microsoft/dotnet:2.1-aspnetcore-runtime-alpine. This image uses alpine, a stripped-down version of linux, but some features are castrated, which need to be tested in actual use, such as internationalization support. It is not enabled in this image by default, and you need to configure environment variables to enable it.

You need to set the following properties of Dockerfile to ensure that it can be copied to the release directory:

3. Build a Docker image

We name the image: aspnetcoredocker

Go to the release file directory and execute the command to package the image:

Docker build-t aspnetcoredocker.

We can write release + build scripts.

Windows:

File name: build.bat

@ echo offecho "Windows Docker build" cd.. / AspNetCore.Dockerdotnet publish-c Release-o.. / publishcd.. / publishecho "publish success" docker build-t aspnetcoredocker.

File name: Linux:

Build.sh

#! / bin/bashecho Linux Docker buildcd.. / AspNetCore.Dockerdotnet publish-c Release-o.. / publishcd.. / publishecho publish successdocker build-t aspnetcoredocker.

The folder structure is shown below:

Perform the build:

. / build.sh

After the construction is successful, you can query the image we built through the docker images command:

If you encounter the error "- bash:. / build.sh: / bin/ bash ^ M: bad interpreter: No such file or directory" when executing the sh script under linux, edit the sh script through vim/vi, press: Jian, enter set ff=unix, then enter and save it with wq.

4. Run Mirror

Run through the command

Docker run-- name=aspnetcoredocker-p 777777 aspnetcoredocker 80-d

-- name: specify the container name

-p: specify the container port

-d: specify the container to run in the background

If a long string appears, it indicates that the operation is successful:

We can also query the container we are running with the docker ps command:

We can also view the startup log through docker logs:

5. Authentication acc

We can access the asp.net core website launched by our container directly through the curl command:

Or access it directly through the browser:

After reading the above, do you have any further understanding of how to run ASP.NET Core 2.1 in Docker? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report