Detailed explanation of Docker, learn it in 7 minutes

Hello everyone, welcome to the Stop Refactoring channel.

This term I read thisdocker.

Docker’s benefit is that it can isolate the environment, many Environmental isolation between containers does not affect each other.

And thanks to the excellent mirroring mechanism, it can be used formanual rapid deployment.

We discuss this in this order:

1. How docker works

2. docker image 

3. docker container 

4. How to create a container

5. Detailed explanation of container creation command 

How docker works

docker is divided into four parts: remote image warehouse, local image, local container, and docker software.

The remote image warehouse is a cloud image warehouse, which allows users to upload and download the required images. The default is dockerhub, but it can also be changed to other warehouses.

Local image is a downloaded image or a self-packaged image. The image can beunderstood asThe template of the container can also be understood as the CD-ROM for the computerto install the operating system.

The local container is the real running instance. The container is created based on the image. Once the container is created, it can be used independently like a Linux system.

The docker software is the base that connects the above three parts and is also the engine that runs the container. Docker can be installed across platforms, and the official has detailed installation instructions, including systems based on Linux, Unix, Windows, etc.

SoGenerallythe same image can be used across platforms, but Windows docker has some limitations.

Docker image

Image can be understood as the CD on which the computer installs the operating system. It isread-only and cannot be modified. The image itself is modified.

There are many kinds of images. Linux images can be used in Linux and Windows docker, and windows images can only be used in windows docker.

The docker image name is composed of two parts, separated by colons. The first is the image name, and the second is the tag version. If the tag version is omitted, the latest version will be selected by default.

It is worth mentioning thatmirrors of different tag versions are completely independent. Technically, they do not rely on associations. More Business connections.

For example, versions 5.7 and 8.0 of mysql have independent tag versions. DockerHub and other image repositories have image usage instructions and tag lists.

Mirror-related commands are shown in the figure: including viewing local mirrors, searching remote mirror warehouses, uploading/downloading mirrors, packaging/importing mirror files, etc.

Docker container

Containers are real running instances. Containers can isolate networks, files, processes and other environments. A container is a sandbox isolation environment.

Docker containers and virtual machine technologies are different.

Docker container is a related container technology based on the system kernel. It does not need to allocate physical resources independently and does not need to start a complete operating system, so the docker container startup speed is faster and lighter Amount.

However, multiple containers share the host's physical resources such as memory and CPU, and mayseize each other's physical resources.

Containers are created based on images. Once the container is created, it can be used like a Linux system.

Modified files in the container will not be synchronously updated to the original image, nor will it affect other containers created through the same image.

If you need to run the modified container on another machine.

The general approach is: first package the container into an image, then upload the image to a remote warehouse or package it into an image file, then download or import the image into the target machine, and finally create a container based on this image.

The relevant commands about containers are as shown in the figure: such as starting and closing containers, packaging containers into images, etc.

It is worth mentioning here that some container settings cannot be modified after the container is created, so they should be set before the container is created.

How containers are created

There are two ways to create a Docker container: one is to create it directly through the command; the other is to create it through dockerfilecreated.

Both are created based on docker images.

But the difference between the two is:Direct creation through commands requires a complete image, dockerfile Incomplete images can be used.

Docker will further improve the container according to the script instructions in the dockerfile, such as downloading files, modifying settings, etc. The container creation is completed only after executing the instructions in the dockerfile.

dockerfile is more flexible, but the container creation takes longer and the stability is not good , especially when you need to download files but the network is not good.

We prefer to use commands to directly create containers, especially in scenarios such as project delivery and rapid deployment. Although the complete image is larger, several gigabytes are common, but it is more stable.

Container creation command

The command to create a container is generally divided into several parts: basic parameters, mounting host directory, network settings, environment variable settings, basic image, and execution of each container startup Orders etc.

Basic parameters is to set the basic parameters of the container, such as specifying the running CPU, memory limit, background startup, container name, etc.

Mounting the host directory maps the host directory to the container. Modifying the files in the directory by the container will affect the host files.

Network settings is to set the container’s network. By default, the container’s network is isolated , that is, 127.0.0.1 in the container points to the container itself, not the host.

Network settings generally set port mapping, such as mapping the host's 8001 to the container's 8080. When accessing the host's 8001 port, it will automatically be forwarded to the container's 8080 port.

If you are sure that the program ports of multiple containers and the host will not conflict,you can set up a shared host network so that the 127.0.0.1 will point to the host, and the port in the container does not require additional mapping.

Docker also provides more complex multi-container network environment settings, but they are not commonly used and will not be expanded upon here.

Environment variable setting is to set the environment variables in the container. The most commonly used setting is to set the time zone. The conventional method of setting environment variables in the container will not take effect.

In addition, the setting of environment variables can also simplify function configuration. For example, MySQL mirroring can configure the initial password through environment variables.

The command executed every time the container is started is the command executed every time the container is started and restarted. It is usually a shell command. When the command execution is completed, the container will automatically stop.

If you want the container to always run without exiting, you can set the executed command to bin/bash .

If the container needs tostart multiple programs, and these programs are running in the background, generally use shell script, start relevant programs in the script, and add /bin/bash at the end of the script to prevent the container from automatically exiting.

It is worth mentioning here that the conventional method of starting the program in the container will not take effect, and the solution is also solved through shell scripts.

Summarize

This issue introduces the basic knowledge and common technical details of docker. Other functions and technical details of docker can be studied in depth when you encounter specific problems.

Docker handles the environment of a single server. If it is container orchestration of multiple servers, additional container cluster orchestration software is required.

Guess you like

Origin blog.csdn.net/Daniel_Leung/article/details/132761732