[Docker common commands]

  • This article records my summary of Docker learning
    Insert image description here

1. Docker basic commands

  • Start docker
systemctl start docker
  • Close docker
systemctl stop docker
  • Restart docker
systemctl restart docker
  • View docker status
systemctl status docker
  • Enable docker to start automatically at boot
systemctl enable docker
  • Turn off docker that starts automatically at boot
systemctl disable docker
  • View docker version number information
docker version
  • View docker system information, including kernel version, number of containers and images
docker info

Insert image description here

2. Docker image command

  • View all local mirror resource information. With the -q option,
    the imageID of all local mirrors is displayed.
docker images
docker images -q
  • Search the docker hub for images with specified conditions, such as searching for images with redis in the image name.
docker search redis
docker search 指定条件

Insert image description here

  • Pull the image on docker hub to local
docker pull 镜像名称
docker pull 镜像名称:tag

Pull the image with the specified name. Image name: tag (version number). Pull the image with the specified name version. If no version number is added, the latest version of the image will be pulled.

  • Delete the specified local image. You can delete multiple images at a time.
docker rmi 镜像名称/镜像id
docker rmi -f 镜像名称/镜像id

By default, the image of a running container cannot be deleted. The related container must be stopped and deleted before its corresponding image can be deleted. But you can also add -f to force deletion.

It is worth mentioning that docker commands can be used together. For example, if we delete all images, we can use the following combination: use the image id queried by docker images -q as the deleted value.

docker rmi $(docker image -q)
  • save image
docker save 镜像名称/镜像id -o 保存的位置与名字

The docker save command can export an image as a tar file and then transfer the image to other servers with docker installed for loading and use.

  • Load image
docker load -i 镜像保存的位置与名字

docker load can import a tar file into an image, and is generally used in conjunction with docker save

3. Docker container commands

3.1. Run the container

  • Containers can be started and run through the docker run command. When this command is executed, it will first search for the specified image locally. If it is found, it will be started directly. Otherwise, it will be searched in the image center. If the image exists in the mirror center, it will be downloaded to the local and started. If the mirror center does not exist, an error will be reported directly.

  • You can view the syntax of the docker run command through the --help of this command. You can see that there are many options. Here are some of the most commonly used ones:

options effect
–name Give the container a name
-i Run the container in interactive mode, usually used together with -t
-t Assign a pseudo terminal to the container
-d Run container as daemon
-h Specify the host of the container
-e Specify the environment required for container startup, such as the connection password
-p Specify port mapping host port:container port
  • Name the container --name
docker run --name 容器名 镜像名称:tag

For example: run the container redis:7.0 and name it redis

docker run --name redis reids:7.0

Insert image description here
As shown in the figure above, it means that the redis container runs successfully, but you cannot type other commands on the console, so we will run the container as a daemon.

  • Run the container as a daemon (run in the background) -d
docker run -d --name 容器名 镜像名称:tag

For example: run the container redis:7.0 as a daemon and name it redis7, and use the docker ps command to view

docker run -d --name redis7 redis7.0

Insert image description here

  • Specify port mapping -p
docker run -d -p 主机端口:容器端口 --name 容器名 镜像名称:tag

For example: run redis:7.0 as a daemon process and name it redis7, and specify the port mapping host port: container port

docker run -d -p 6380:6379 --name redis7 redis7.0

Insert image description here
The above command will map port 6379 of the container to port 6380 of the host.
Note: When the docker container is started, if the port mapping parameters are not specified, network applications and services in the container cannot be accessed through the network from outside the container. Through port mapping, we can access the specified port of the host from the outside to access the container application.

  • Run the container in interactive mode and assign a pseudo-terminal to the container -it
docker run -it 镜像名称:tag /bin/bash

For example: run the centos:7.6.1810 container in interactive mode, assign a pseudo terminal to the container, and then name the container

docker run -it --name centos7 centos:7.6.1810 /bin/bash

Insert image description here
Exit the terminal and enter exit. In the terminal, some Linux commands are also applicable.
Note: If you use exit to exit the container, the container will stop directly. If you use the Ctrl+P+Q command to exit, the container will not stop (commonly used).

-i : Run the container in interactive mode
-t : Allocate a pseudo terminal to the container (generally used in combination with the two)
/bin/bash: The command placed after the image name is the command. Here we hope to have an interactive shell, so use is/bin/bash

You can also use -itd at the same time, for example

docker run -itd --name centos7 centos:7.6.1810

Insert image description here
When this command is executed, a container ID will be returned, because executing the -d command returns a container ID.

Question: Why use -itd?
When we docker run -d, some images create containers, we find that the container is not created successfully through docker ps. For example, when we create the centos7 container, we find that it is not created successfully after docker ps. Note: If the docker container runs in the background, there must be a foreground process
Insert image description here
. . If the commands run by the container are not those that have been suspended (such as running top, tail), they will automatically exit.

This is a problem with the docker mechanism, such as web containers, taking nginx as an example. Under normal circumstances, to configure the startup service, you only need to start the corresponding service, such as service nginx start. But in this way, nginx runs in background process mode, resulting in no running applications in the docker foreground. After such a container is started in the background, it will automatically commit suicide because it thinks it has nothing to do.

The solution is to run the program you want to run as a foreground process, and use -it to provide a pseudo terminal, indicating that I still have interactive operations to do.

3.2. Exit the container

Exiting a container refers to exiting a container running in interactive mode. There are two exit methods:
exit and stop the container : exit
exit but do not stop the container : Ctrl+P+Q

3.3. View container processes and logs

  • List all containers, including currently running and stopped ones
docker ps -a
  • List the latest created containers, whether they are running or not
docker ps -l
  • List the specified number of newly created containers
docker ps -n
  • Only the container ID is displayed, which must be used together with the previous options, such as with -a.
docker ps -aq  //查询出所有容器的ID
  • View the processes running in the container
docker top 容器名称/容器ID
  • View container logs
docker logs 容器名称/容器ID
  • View container details
docker inspect 容器名称/容器ID

Insert image description here

3.4. Enter the container again

When we run a container as a daemon, or run a container in interactive mode, but the log command executed inside the container occupies the interactive command line (just like running tomcat in interactive mode), and at this time we want to enter Go to the container to perform some operations inside the container. At this time, you need to use the exec/attach command (exec is recommended).

  • Enter the container using the container process
docker attach [OPTIONS] 容器名称/容器ID

For example: use docker attach centos7 to enter the container.
Insert image description here
This command does not create a new process, but uses (attaches to) the container process. Use the exit command to exit the current process and stop the container. As shown in the figure above, after exiting using the exit command, query again and find that the centos container is no longer running.

  • Create a new process and enter the container
docker exec [OPTIONS] 容器名称/容器ID COMMAND

For example: use docker exec -it centos7 /bin/bash to enter the container and open a new bash terminal.
Insert image description here
This command will create a new process independent of the container, and using the exit command is only used to end the new process. As shown in the figure above, after using the exit command to exit, query again and find that the centos container is still running.

3.5. Container start and stop

  • Start container
docker start 容器名/容器ID
  • Restart container
docker restart 容器名/容器ID
  • Stop container
docker stop 容器名/容器ID

This command stops the container. If the container is being accessed by other processes, it will be stopped after the access is completed.

  • Force stop container
docker kill 容器名/容器ID

This command stops the container, regardless of whether the container is accessed by other processes.

3.6. Container deletion

  • By default, this command requires that the container to be deleted must be a stopped container. Multiple containers can be deleted at the same time.
docker rm 容器名/容器ID
  • Adding -f to this command can force the container to be deleted, regardless of whether it is stopped or not.
docker rm -f 容器名/容器ID

3.7. Container and host file transfer

The docker cp command can copy files/directories between the container and the host, regardless of whether the container is running.

  • Copy files from the container to the host
docker cp 容器名称/容器ID:容器路径 目的主机路径
docker cp centos7:/opt/hello.txt /opt/

For example: Copy the hello.txt file in the /opt directory of the centos7 container to the host's /opt directory while the container is not stopped.
Insert image description here

For example: when the container is stopped, copy the docker.txt file in the /opt directory of the centos7 container to the /opt directory of the host machine.
Insert image description here

  • Copy files from the host to the container
docker cp 主机路径 目的容器名称/容器ID:容器路径
docker cp /opt/aaa.txt centos7:/opt/

For example: copy the host/opt/aaa.txt file to the container’s /opt/
Insert image description here

3.8. Export and import containers

  • Export the container as a compressed file
docker export 容器名称/容器ID > 目的路径/[文件名.tar|文件名.tar.gz]

For example: export the centos7 container as a test.tar.gz compressed file under /opt/
Insert image description here

  • Make compressed files imported as containers
docker import 文件路径/[文件名.tar|文件名.tar.gz] 镜像名:tag

Small case:
1. Delete the centos image first
. 2. Execute the command docker import test.tar.gz centos:7.6.1810
3. Check the image and find that the image has been generated.
4. Run docker run and find that it is successful.
Insert image description here

3.9. Submit an image into a new image copy

  • Submit an image into a new image copy
docker commit -m "描述信息" -a "作者" 容器名称/容器ID 镜像名:tag

Small case:
1. yum install vim in the original centos container
2. Submit the original image into a new image copy
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/qq_53354183/article/details/127884484