In the combination of the two, Microsoft official gave a lot of support, from the official release of some articles and VS 2017 in the establishment. NET Core project comes with Docker options can be seen, which also has a lot to do with the cross-platform characteristics of Core, and Docker can choose to deploy in Linux or Windows environment

Add Dockefile

The last article covered how to pull the official image of Core, but we will eventually have to apply Docker to our development environment. How do we generate and deploy our own projects to Docker? The first step is to add a Dockerfile file. In VS2017, there are roughly three methods, which are slightly different, but ultimately also create a Dockerfile file

1. Add it when creating the project

When creating a Core project, select Enable Docker support. A DockerFile file is automatically added to the new project. The details of the file are analyzed in the following sections

2. Add it manually

Projects that are already in use can also be created by right-click-add-Docker support

3. Container business process coordination and control program support

This approach is more special than the first two. It is no longer just adding a Dockerfile file, but as the name says, a whole production chain, which is used to coordinate the continuous integration tool’s development-debug-generation-release one-stop service. The method of adding is the same as the second method, right-click the project add to see the mouthful name

In addition to generating a Dockerfile, this method adds a business process coordinator called Docker Compose to the solution. You can select Docker Compose when you create it, but it is the only one that comes with it by default. It contains two files, one is.dockerignore, which is similar to Git, and the other is docker-comemess. yml, which is used to configure information about the business process, such as the image name and the path of the Dockerfile

Dockefile grammar

There are probably the following, pick a few used to say

FROM
MAINTAINER
RUN
CMD
EXPOSE
ENV
ADD
COPY
ENTRYPOINT
VOLUME
USER
WORKDIR
ONBUILDCopy the code
  • FROM

FROM <image>

Note If no local image is selected, latest is displayed by default if no label is specified

The FORM directive is the first line of the Dockerfile file, but can be more than one

Take the default Core project as an example. The images pulled here are the official images of Core, which were also useful in the previous article: the runtime and SDK

  • WORKDIR

WORKDIR < working directory path >

The path to which the image was installed is obvious. If it doesn’t exist, Docker will create it automatically

  • COPY

COPY < source path > < destination path >

Copies files and directories to the container’s file system. Files and directories need to be in paths relative to Dockerfile.

  • RUN

RUN <Shell/exec>

Commands to be executed on the current image can be in shell or exec format

  • EXPOSE

EXPOSE < port >

Local port mapped by the server container

  • ENTRYPOINT

Use the same format as RUN, but this command is executed after the container is started, not overwritten by RUN command, a Dockerfile can only have one, if there are more than one, only the last one is executed

The rest of the instructions have not been used, the usage is not clear

Docker project debugging

We mentioned three ways to create a Dockerfile earlier, but there are actually two cases, and for each case, there are different ways to package the image

Add files only

Use the docker build -t
directive. This case is more general and can be used to package images whether they are created with VS or not

Go to the path where the Dockerfile file is stored and run the following command

In order to demonstrate, I first deleted the official SDK of Core. Since we wrote FROM command in Dockerfile, we found that Docker automatically downloaded the image and packaged our project after executing the command. Image operating system “Windows” cannot be used on this platform, because my Docker uses Linux mode. Here we build the container image of Windows. Therefore, you need to switch the tray icon in the lower right corner of the system to right-click “Switch to…” .

In order to show the effect of Dockerfile instruction, we delete the image created before and execute the Build instruction again. Docker did not download the SDK of Core this time, because it has been downloaded before. However, I ran into network problems and found that the previous image pull failed, so I changed the image acceleration address, simplified the dockerfile, and repeated the above operation

FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
COPY . .
EXPOSE 80
ENTRYPOINT ["dotnet", "CoreDockerDemo1.dll"]Copy the code

You can see that the instructions in dockerfile are executed in sequence. After completion, we can use docker image LS to see the image we built. Then we can use the above method to create docker

Container business process coordination control program support

This way you don’t need to build by hand, just click the debug button in VS. Since we added this coordination control program before, we can directly select Docker for debugging in this project

Docker-comemess. yml file is basically similar to dockerfile, and more intuitive, corresponding to the input name and so on

The error “volume sharing is not enabled” appeared in the first generation. Here, we need to select the disk built by program generation in the Shared Drives TAB of Docker Settings, and then click “Apply” button to Apply the Settings. Then Docker will automatically restart

After setting this up, click Run in VS, and the Web application will automatically compile and create images and containers, and then launch the site. The first time you start up, you may be asked if you want to authorize SSL certificates


Basically, with these, Docker can be used to bring some convenience to the development work. If we want to go further, it is to combine Docker with continuous integration and apply it to the website server environment