Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to run ASP.NET Core 2.1 in Docker

Shulou Source: shulou.com Published: 2022-06-02 06:45:25 10月05日 Update

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.

Tags: Run mirror command container file can pass content script project success file name directory support query minimum function variable name background character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information OPPO Reno Shulou Technology Apple