[ASP.NET Core 3 framework Secret] cross-platform development experience: Docker

For a .NET Core developer, you probably have not used Docker, but you can not never heard of Docker. Docker is one of the most popular open source projects on Github, it claims to be the cornerstone of all cloud applications, and to upgrade to the next generation Internet. Docker is dotCloud company open source a product, from the moment of birth date, in just two to three years to become the most popular open source community project. For fully embrace open source .NET Core, it should naturally provide the perfect support for Docker. For what follows, we assume that you already have a basic understanding of the Docker, and on your machine (Windows) Docker already installed.

First, create a ASP.NET Core Applications

We will demonstrate how to create an ASP.NET Core program and compile it into a Docker mirror, and Docker environment to start an application example for the mirror to create a container. For simplicity, we take the form of direct scaffold command line to create the ASP.NET Core applications. Shown in Figure 1, we execute dotnet new web command "d: \ projects \ helloworld" to create an empty directory ASP.NET Core applications.

1-23

Second, the definition Dokerfile

We now need to use the ASP.NET Core Docker made into a mirror, for which we need to create a project in the root directory Dockerfile file (the file name is Dokerfile, no extension), and define the following elements in the file. If we have a basic understanding of Dockerfile, for the contents of this document should not be difficult to understand.

# 1 . Specify the mirror compile and publish applications
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build

# 2 Specify (compiling and publishing) working directory
WORKDIR /app

# 3 . .Csproj copied to the working directory / app, and then perform dotnet restore restore all packages installed NuGet
COPY *.csproj ./
RUN dotnet restore

# 4 . Copy all the files to the working directory (/ app), then execute the command dotnet publish publish applications to / app / out directory
COPY . ./
RUN dotnet publish -c Release -o out

# 5 . Mirror compiled Docker
# 5.1 . Set the base image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime

# 5.2 . Settings (run) the working directory, and the files are copied to publish out subdirectory
WORKDIR /app
COPY --from= build /app/out .

# 5.3 . Use environment variables listen addresses ASP.NET Core applications
ENV ASPNETCORE_URLS http://0.0.0.0:3721

# 5.4 . Dotnet execute command to start the ASP.NET Core Applications
ENTRYPOINT ["dotnet", "helloworld.dll"]

This Dockerfile using an intermediate layer (build) for temporarily storing resources after ASP.NET Core MVC application release, its working directory "/ app". Specifically, this layer is made " in the Microsoft / aspnetcore-Build: 2 " as the base image, we first define the .csproj project file (helloworld.csproj) copy of the current working directory, then run "dotnet restore" command to restore all registered NuGet package in this project file. Next we will copy all files in the current working directory to the current project, and the implementation of dotnet publish the entire project compile release (for Release mode), after the release of resources is saved to the directory "/ app / out" in.

When the compiler generates real Docker image, we use " mcr.microsoft.com/dotnet/core/aspnet:3.0 " as a base image, all files due to a pre-release applications on it, so we only need to publish copy of the current working directory on it. Next we set up the monitoring address ASP.NET Core applications (http://0.0.0.0:3721) environment variables. For the definition of ENTRYPOINT (ENTRYPOINT [ "dotnet", " helloworld.dll"]), we know that when the container is activated, "dotnet helloworld.dll" command is executed to start the ASP.NET Core applications.

Third, generating a mirror

After a good Dockerfile file definition, we open the CMD command prompt and change to the project where the root directory (that is, the directory Dockerfile file is located), then perform "docker build -t helloworldapp." Command, which will use this to generate a file named Dockerfile as helloworldapp "the Docker image.

1-24

Fourth, start the container

Since Docker image has been created out successfully, then the rest of the work is very simple, we just need to create the corresponding container for this image, start the final ASP.NET Core applications can be done directly by the start of the container. As shown below, we perform " Docker RUN -d -p 8080: 3721 --name myapp helloworldapp " command generated Docker in front of a mirror (helloworldapp) to create and start a named myapp against (- name myapp) container. Since we access the application from the outside, so we mapped through the port (-p 8080: 3721) 3721 internal listening port mapping for the current host machine port 8080, so we use the address "http: // localhost: 8080" Access this by ASP.NET Core docker container carrier applications.

1-25

[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [Part]
[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [novellas]
[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [next]
[3 Core ASP.NET framework Secret] cross-platform development experience: Mac OS
[3 Core ASP.NET framework Secret] cross-platform development experience: Linux
[3 Core ASP.NET framework Secret] cross-platform development experience: Docker

Guess you like

Origin www.cnblogs.com/artech/p/inside-asp-net-core-01-06.html
Recommended