Tips: This article has been added to the reading list of series articles. You can click to see more related articles.
Docker is an open source application container engine, it is very popular, now almost become a skill that the back-end developers must master. Even if you may not use it in the production environment, it is very convenient to use it as an auxiliary development tool. This article introduces some basic uses of. Net core application in docker.
First install docker and download it from the official website https://www.docker.com/get-started
Windows system, the default win10 (better than the new version, can use WSL). Win7 can only use docker toolbox. There are many bugs. It is not recommended to use…
Windows / Mac directly download the corresponding files to install (the system environment of this paper is win10 2004 version.) Linux can be installed by command. About installation, there are a lot of information on the Internet, so I won’t introduce it. There are only two necessary settings after installation
There are many websites that provide free acceleration service, you can search by yourself. I use alicloud here. This domestic network must be configured, or the image can hardly be pulled down.
- Image storage path
By default, docker’s running file is on disk C. If Disk C is not enough, it can be set to another disk.
The new version of windows docker desktop uses WSL by default
This setting is troublesome. Please refer to my previous article: win10 uses wsl2 to run docker desktop, and the running files are migrated from disk C to other directories. If the old version of Hyper-V starts, the settings on the interface are OK.
Basic concepts of docker
The two most important concepts in docker are “mirror image” and “container”.
Image is a unified view of a bunch of read-only layers.
As like as two peas, image is almost the same as container, and it is also a unified view of a stack. The only difference is that the top layer of the container is readable and writable.
It is not easy to understand the difference between a docker image and a docker container.
Imagine the one we developed asp.net MVC application, after using visual studio to publish it, will get a pile of published files, including DLL, cshtml, CSS, JS, static resource files and so on. Then this heap of files is similar to a mirror image, which cannot be run directly. When we mount this heap of files to a site of IIS, it can be run and can be accessed by the outside world. The IIS site is similar to a container. The container is equivalent to a running instance of the mirror image. It should be noted that all read and write operations of the container are only for the container’s file system and will not affect the image. A mirror can run multiple containers, and containers are isolated from each other.
Docker basic command
- Mirror related:
docker images: lists all local mirrors.
Docker RMI image... | image ID: delete the local image. Multiple images can be specified at the same time.
docker build -t myimage:1.1 .: build an image named myimage and tag 1.1 based on dockerfile. The ending. Represents the current directory.
docker pull mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04: pull the SQL Server 2019 image with tag 2019-cu5-ubuntu-18.04 from Microsoft image warehouse.
- Container related:
docker ps: lists the running containers.
docker ps -a: lists all containers, including those that are not running.
docker run -it --rm -p 8080:80 --name mynginx nginx: use mirror nginx to create and start a container named mynginx; – it means to start in interactive mode and reallocate a pseudo input terminal for the container; – P specifies the port mapping to map port 80 of the container to port 8080 of the host; — RM means to delete the container automatically when the container stops.
docker run -d -p 8080:80 -v /nginx/data:/data --name mynginx nginx:latest: create and start a container named mynginx by using the mirror nginx (tag is latest); – D represents background mode startup; – P specifies port mapping to map port 80 of container to port 8080 of host; – V represents mount volume, and mount host’s / nginx / data directory to container’s / data directory.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=" -d -p 1433:1433 --name sqlserver2019 mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04: create and start a container named sqlserver2019 using sqlserver image (tag: 2019-cu5-ubuntu-18.04); – D represents background mode startup; – P specifies port mapping to map 1433 port of container to 1433 port of host; – E is the specified environment variable.
Docker start / stop / restart container name... | container ID: start, stop and restart containers respectively. Multiple containers can be specified at the same time.
Docker RM container name... | container ID: delete a container. Multiple containers can be specified at the same time.
Docker logs container name | container ID: View container log.
Docker command practice
Next, start a container with sqlserver2019 image in docker.
- Pull image:
docker pull mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04
be careful, mcr.microsoft.com It is the mirror source of Microsoft, and the domestic visit will be slow…
- Start container:
docker run -e "ACCEPT_EULA=Y" -e "[email protected]" -d -p 1433:1433 --name sqlserver2019 mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04
- Connection test
There are many tools to connect to database test by using visualization tools. I use SQL Server Management here (I can also directly use commands in docker to enter container connection database test).
Use account SA/ [email protected] connect. The server name is localhost, 1433, which can also be omitted. The default port is 1433.
The test connection was successful. Through the above two commands, you can get a SQL Server 2019 database, is it more convenient than directly installing it on the computer. Many similar development environments can be built in this way, such as mysql, Postgres, redis, mongodb, rabbitmq, and so on. All kinds of tools can be tossed around at will. If they are broken, the container will be deleted and it will be done again…
Build docker image
Let’s use docker to build a asp.net The image of core web application.
First create a new one using vs2019 asp.net Core web application, select web API as project template.
Right click the project – add – docker support, and select Linux as the target OS.
After adding docker support, vs2019 will automatically help us create dockerfile files. Dockerfile is the file used to build the image, which contains various instructions. The following is a detailed explanation of the dockerfile command:
#Use asp.net Core 3.1 is used as the base image, and its alias is base FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base #Set the working directory of the container to / APP WORKDIR /app #Exposed 80 ports EXPOSE 80 #Use. Net core SDK 3.1 as the base image, and create an alias as build FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build #Set the working directory of the container to / SRC WORKDIR /src #Copy the webapplication1 / webapplication1.csproj project file to the / SRC / webapplication1 / directory in the container COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"] #Execute the dotnet restore command, which is equivalent to restoring the nuget package with vs RUN dotnet restore "WebApplication1/WebApplication1.csproj" #Copy the files from the current directory to the / SRC directory of the container COPY . . #Set the working directory of the container to / SRC/WebApplication1 WORKDIR "/src/WebApplication1" #Execute dotnet build command, which is equivalent to using vs to build project. Generate the / APP / build directory to the container in release mode RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build #Take the above build (. Net core SDK 3.1) as the base image and rename it to publish FROM build AS publish #Executing the dotnet publish command is equivalent to using vs to publish projects. Publish to the container's / APP / publish directory in release mode RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish #Set the base above（ asp.net Core 3.1) as the base image and renamed as final FROM base AS final #Set the working directory of the container to / APP WORKDIR /app #Copy the / APP / publish directory to the current working directory COPY --from=publish /app/publish . #Specifies the container entry command. Dotnet webapplication1.dll will be run when the container starts ENTRYPOINT ["dotnet", "WebApplication1.dll"]
The content is very long. In fact, the main thing to do is to define a series of packaging, publishing and running processes through the. Net core cli command.
bulid & run
Go to the root directory of the project, start PowerShell or CMD and execute the docker command.
docker build -t webapp1 -f ./WebApplication1/Dockerfile .The – f parameter specifies the directory where the dockerfile is located.
docker imagesCheck the local image. Webapp1 is the image built above
docker run -d -p 5000:80 --name web1 webapp1
docker psTo view a running container:
Browser access: http://localhost :5000/weatherforecast
So far, a simple asp.net The core web application runs successfully in docker.
In fact, vs2019 itself supports docker very well. The above operations can be completed directly in vs2019 without manually executing the docker command.
Set the project to docker start:
CTRL + F5 to start:
Note that container tools are slow to load for the first time… After startup, the browser will be opened automatically, and a random port is bound:
This is the container that vs2019 automatically creates for us:
Viewing the output log of the container tool in vs2019, you can see the instruction content executed by vs2019.
This article begins with this.