A preliminary exploration of the use of Docker (1): common instructions

Posted Jun 15, 20207 min read

I have practiced it a few weeks ago, and if I don t record it, I will forget it.

Docker is the container technology, there are already many specific introductions, I won t go into details, and talk about some advantages

Why use Docker

For me personally, the biggest advantage is that you can deploy different environments on one computer without worrying about conflicts. The most common conflict is port occupation. Using Docker technology can easily circumvent this problem, and it is easy to manage. , I don t have to deal with multiple environments locally

There are many more benefits

  • More efficient use of system resources

As you will see below, even if I run multiple environments on a common computer at the same time, it will not occupy too much resources on the host machine, and normal development can still be carried out, not to mention a dedicated server host

  • Faster startup time

Traditional virtual machine technology usually takes a few minutes to start application services. Docker container applications, because they run directly on the host kernel and do not need to start a complete operating system, can achieve startup times of seconds or even milliseconds. Greatly save the time of development, testing and deployment.

  • Consistent operating environment

A common problem in the development process is the issue of environmental consistency. Due to the inconsistency of the development environment, test environment, and production environment, some bugs were not discovered during the development process. The Docker image provides a complete runtime environment in addition to the kernel, ensuring the consistency of the application's operating environment, so that there will be no more This code is fine on my machine Such problems.

  • Continuous delivery and deployment

For development and operations(DevOps) personnel, the most desirable is to create or configure at a time, can run normally in any place.
Using Docker, you can achieve continuous integration, continuous delivery, and deployment by customizing application images. Developers can use Dockerfile to build the image and integrate it with the Continuous Integration(Continuous Integration) system for integration testing, while the operation and maintenance personnel can quickly deploy the image directly in the production environment, or even combined with the continuous delivery(Continuous Delivery/Deployment) system. Perform automatic deployment.

  • Easier migration

Because Docker ensures the consistency of the execution environment, it makes application migration easier. Docker can run on many platforms, whether it is a physical machine, a virtual machine, a public cloud, a private cloud, or even a laptop, the results are consistent. Therefore, users can easily migrate applications running on one platform to another platform without worrying about changes in the operating environment that cause the applications to fail to run normally.

  • Easier maintenance and expansion

The hierarchical storage and mirroring technology used by Docker makes it easier to reuse the repeated parts of the application, it also makes the maintenance and update of the application easier, and it is also very simple to further expand the image based on the basic image. In addition, the Docker team maintains a large number of high-quality official images together with various open source project teams, which can be used directly in the production environment or can be used as a basis for further customization, which greatly reduces the cost of image production for application services.

Docker installation and simple use

I won t talk about the basic things. The installation of different operating systems depends on this.
Docker-from entry to practice . I mainly record some main points in the article, as well as the specific operations on the windows system

Domestic mirror acceleration

Like npm, domestic network problems, the speed of pulling the mirror is relatively slow, so to configure the domestic mirror, you can use Docker official or other cloud service providers

I am using a Windows 10 system. The configuration method is to select Settings in the right-click menu in the Docker icon in the tray at the bottom right corner of the system. After opening the configuration window, select Daemon on the left navigation menu. Fill in the accelerator address https://registry.docker-cn.com in the Registry mirrors column, then click Apply to save and Docker will restart and apply the configured mirror Address.

Common instructions

After installing Docker, use the cmd command to open the command control line to use the Docker command. At present, I have only done some simple practices, record the instructions I use more frequently

1. docker ps -a

First of all, you need to check which containers are currently installed

This command will list the status of all containers, container ID, name, used image, mapped address, etc. I personally think it is the one with the highest frequency of use

2. docker pull [NAME]

Similar to npm, if you want to create a container, you need to first select the image and pull it to the local, find the required image in Docker Hub , or make the image yourself and use The docker pull command pulls to the local, taking the official sample image of .NET Core as an example

docker pull microsoft/dotnet-samples

After the command is executed, wait for the pull to complete, and pull the same image again to update

3. docker image ls

View the mirror of this machine

After pulling, check the status of the local image, which will list the repository(Repository), label(usually the version number), image ID, creation time, and image size from which the image comes.

4. docker [image/container]rm [ID/NAME]

Delete the image or container, the usage of the rm command is the same for the container and the image, the main difference is that the image has no name

If you want to delete the image by name, you can do this docker image rm :

If you want to delete the mirror by ID, you can enter the complete ID, or you can use the short ID, short ID is to enter only the first few characters of the ID, generally more than three can distinguish different mirrors

It should be noted that if there is a sub-container that depends on the image, it cannot be deleted, and it cannot be deleted while the container is running, but you can add the -f parameter, so that Docker will send a SIGKILL signal to the container

In addition, all container instructions can be omitted, docker rm [ID/NAME] is to delete the specified container

5. docker run []

Create and start a container, this command is more important and more commonly used parameters, here use the command given by Microsoft official image as an example

docker run -it --rm -p 8000:80 --name aspnetcore_sample microsoft/dotnet-samples:aspnetapp
  • The -t option tells Docker to assign a pseudo-tty and bind it to the standard input of the container
  • -i leaves the standard input of the container open
  • --rm should be combined with the previous it parameter, mainly for testing. After adding this parameter, when we use Ctrl+C to close the terminal, this container will be automatically deleted
  • -p port mapping, 8000:80 means to map the container's 80 port to the host's 8000 port, which means that accessing my local 8000 port is equivalent to accessing our deployment environment's 80 port
  • --name The name of the container
  • The last one is the name of the mirror, mentioned earlier, :

After executing this command, docker will first see if there is a corresponding image. If not, it will automatically perform a pull operation. We have previously pulled the official Microsoft Core application image. Here we start a Web sample image, so there will be a pull Fetch operation, need to wait for a while

This is how the creation is successful and started. Visit http://localhost:8000 to see the Core sample website

If you remove the --rm parameter, the container will not be deleted after exiting. You can start it again with the start command below. If you do not use the parameter, you will not enter the terminal again.

If there is a problem, you can use the first command ps mentioned earlier to see the status of the current container

6. docker start [ID/NAME]

When we restart docker, under normal circumstances, the container will be in the Exited exit state. At this time, it needs to be started by the start command. As mentioned earlier, the ID can be long or short. The name is --name to create the container Parameterized

Here is an example of the container that has been closed above

Returning the container ID is generally a successful start, so that you can continue to access through port 80

7. docker exec

There is another instruction to enter the container docker attach The two are slightly different, here will explain the reason below

docker exec can be followed by multiple parameters, here mainly describes -i -t parameters.

The run command was also mentioned earlier. It is similar here. When only the -i parameter is used, the interface does not have the familiar Linux command prompt because the pseudo terminal is not assigned, but the command execution result can still be returned. . When the -i -t parameters are used together, you can see the familiar Linux command prompt.

But I don't know if it is a wrong command. You can only follow it with a simple command like ls. If you use cd [directory name] this kind of command with spaces cannot be recognized, so use bash command can enter the linux terminal to use linux commands normally

The next section will talk about how to deploy .NET Core on Docker, which is no longer an official example, but how to upload an image from a development environment to a deployment environment, how to build an image through a Dockerfile to the warehouse