. NET Core multi-platform development experience [4]: Docker and coredocker
For a. NET developer, you may have never used Docker, but you have 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 upgrade the Internet to the next generation. Docker is an open-source product of dotCloud. Since its birth, Docker has become the most popular project in the open-source community in just two or three years. For. NET Core, which fully embraces open source, it should naturally provide perfect support for Docker. For the following content, we assume that you have a basic understanding of Docker and installed Docker on your machine.
We will demonstrate how to create an ASP. NET Core MVC application and compile it into a Docker image. Finally, we will create the corresponding container for the Docker environment of this image. For simplicity, we still directly use the command line scaffolding to create this ASP. NET Core MVC application. As shown in, run the dotnet new mvc command to create an ASP. NET Core MVC application named helloworld under the "d: \ projects" directory.
FROM microsoft/aspnetcore-build: 2.0 AS build-envWORKDIR/app # copy. csproj to the working directory/app, and then execute dotnet restore to restore all installed NuGet packages. COPY *. csproj. /RUN dotnet restore # COPY all files to the working directory (/app), and then RUN the dotnet publish command to publish the application to the/app/out directory COPY .. /RUN dotnet publish-c Release-o out # compile the Docker image FROM microsoft/aspnetcore: 2.0 WORKDIR/appCOPY -- from = build-env/app/out. ENV ASPNETCORE_URLS http: // 0.0.0.0: 3721 ENTRYPOINT ["dotnet", "helloworld. dll "]
This Dockerfile uses an intermediate layer (build-env) to store post-release resources of ASP. NET Core MVC applications. Its working directory is "/app ". Specifically, this layer uses "microsoft/aspnetcore-build: 2.0" as the basic image. We will first define the project. csproj file (helloworld. csproj) copy to the current working directory, and then run the "dotnet restore" command to restore all the NuGet packages registered in this project file. Next we will copy all the files in the current project to the current working directory, and execute dotnet publish to compile and Release the entire project (for the Release mode ), the released resources are stored in the/app/out directory.
When ASP. when the NET Core MVC application is compiled into a Docker image, "microsoft/aspnetcore: 2.0" is used as the basic image. Because the application is pre-released, so we only need to copy all the published files to the current working directory. Next, we set the listening address (http: // 0.0.0.0: 3721) of the ASP. NET Core MVC application through the environment variable ). For ENTRYPOINT definition (ENTRYPOINT ["dotnet", "helloworld. dll "]), we know that when the container is started," dotnet helloworld. dll command will be executed to start this ASP.. NET Core MVC application.
After the Dockerfile is defined, open the CMD command line. After switching to the root directory of the project (that is, the directory where the Dockerfile is located), Run "docker build-t helloworldapp. "command to generate a Docker Image Based on the Dockerfile and name it" helloworldapp ".
Since the Docker image has been successfully created, the rest of the work is very simple. We only need to create the corresponding container for this image, the final ASP. NET Core MVC application can be started directly by starting the container. As shown in, run the "docker run-d-p 8080: 3721 -- name myapp helloworldapp" command for the Docker image (helloworldapp) created above) create and start a container named myapp (-- name myapp. Because we access this application from outside, we map the internal listening port 8080 to the port 3721 of the current host machine through port ing (-p 3721: 8080, therefore, the request address "http: // localhost: 8080" accesses the ASP.. NET Core MVC application.
. NET Core multi-platform development experience
. NET Core multi-platform development experience [1]: Windows
. NET Core multi-platform development experience [2]: Mac OS X
. NET Core multi-platform development experience [3]: Linux
. NET Core multi-platform development experience [4]: Docker