Docker container management

1. Create a container

Container creation: It is the process of loading the image into the container.

If no container name is specified when creating the container, the system will automatically create one.

The newly created container is in the stopped state by default and does not run any programs. A process needs to be initiated in it to start the container.

The container created by docker create is not actually started, and the docker start or docker run command needs to be executed to start the container.

格式: docker create [选项] 镜像名 <命令>
 常用选项:
 #-t 选项让 Docker 分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,-i则让容器的标准输入保持打开。-it,在交互模式下,用户可以通过所创建的终端来输入命令。
 #更多的命令选项可以通过 man docker -run 命令来查看

docker create -it nginx:latest /bin/bash   #创建容器时如果没有指定容器名称,系统会自动生成一个名称
 docker create -it --name=test01 nginx:latest /bin/bash  #创建容器并指定名称为test01
 ​
 [root@yuji ~]# docker create -it nginx:latest /bin/bash
 9fec235e6279899b5198e45dd98c60bd7d4f7cd1f9be8d1922eb775e45693ace
 [root@yuji ~]# docker create -it --name=test01 nginx:latest /bin/bash
 a55669e587d57a8dc64725c9016b6ce72b941bad3fd5c7d21d99355ea8a7c801
 [root@yuji ~]# docker ps -a
 CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS    PORTS     NAMES
 a55669e587d5   nginx:latest   "/docker-entrypoint.…"   4 seconds ago   Created             test01
 9fec235e6279   nginx:latest   "/docker-entrypoint.…"   3 minutes ago   Created             eloquent_goldberg

 2. View the running status of the container

格式:
 docker  ps [选项]
 ​
 docker ps        #查看当前运行状态的容器
 docker ps -q     #只显示运行状态的容器的ID
 docker ps -a     #-a 选项可以显示所有的容器
 docker ps -aq    #只显示所有容器的ID
 docker ps -as    #显示所有容器的大小
 docker ps -n 2   #查看最后创建的2个镜像

[root@yuji ~]# docker ps       #查看当前运行状态的容器
 CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS          PORTS     NAMES
 a55669e587d5   nginx:latest   "/docker-entrypoint.…"   8 minutes ago   Up 27 seconds   80/tcp    test01
 [root@yuji ~]# docker ps -a    #查看所有的容器
 CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS     NAMES
 a55669e587d5   nginx:latest   "/docker-entrypoint.…"   8 minutes ago    Up 53 seconds   80/tcp    test01
 9fec235e6279   nginx:latest   "/docker-entrypoint.…"   11 minutes ago   Created                   eloquent_goldberg
 [root@yuji ~]# docker ps -aq     #只查看所有容器的ID
 a55669e587d5
 9fec235e6279

 3. Start the container

格式:docker start 容器的ID/名称
 ​
 #查看容器ID
 docker ps -a
 #使用容器ID,启动容器
 docker start 9fec235e6279
 ​
 [root@yuji ~]# docker ps -a     #查看容器ID
 CONTAINER ID   IMAGE          COMMAND                  CREATED             STATUS          PORTS     NAMES
 a55669e587d5   nginx:latest   "/docker-entrypoint.…"   About an hour ago   Up 55 minutes   80/tcp    test01
 9fec235e6279   nginx:latest   "/docker-entrypoint.…"   About an hour ago   Created                   eloquent_goldberg
 [root@yuji ~]# docker start 9fec235e6279      #使用容器ID,启动容器
 9fec235e6279
 [root@yuji ~]# docker ps -a  
 CONTAINER ID   IMAGE          COMMAND                  CREATED             STATUS          PORTS     NAMES
 a55669e587d5   nginx:latest   "/docker-entrypoint.…"   About an hour ago   Up 56 minutes   80/tcp    test01
 9fec235e6279   nginx:latest   "/docker-entrypoint.…"   About an hour ago   Up 3 seconds    80/tcp    eloquent_goldberg

4. Create and start the container

You can directly execute the docker run command, which is equivalent to executing the docker create command first and then the docker start command.

Note: The container is a terminal that coexists with the shell command running in it. The command runs the container to run, and the command ends the container to exit.

By default, the docker container will use the first process inside the container, that is, the program with pid=1, as the basis for whether the docker container is running. If the process with pid=1 in the docker container hangs up, the docker container will exit directly, that is, It is said that there must be a foreground process in the Docker container, otherwise the container is considered to have hung up.

When using docker run to create a container, the standard running process of Docker in the background is:

(1) Check whether the specified image exists locally. When the mirror does not exist, it will be downloaded from the public repository;

(2) Create and start a container using the image;

(3) Assign a file system to the container, and mount a read-write layer outside the read-only image layer; (The image is read-only and cannot be deleted.)

(4) Bridge one virtual machine interface from the bridge interface configured on the host host to the container;

(5) Assign an IP address in an address pool to the container;

(6) Execute the application program specified by the user, and the container will be terminated after the execution is completed.

docker run [选项] 镜像 [命令] [参数...]

#如果本地没有该镜像,会自动去仓库拉取镜像。
 #启动容器后执行"ls /"命令,执行完命令后退出容器
 docker run centos:7 /usr/bin/bash -c ls /     #"/usr/bin/bash -c 命令":启动容器时加命令
 ​
 docker ps -a     #会发现创建了一个新容器并启动执行一条 shell 命令,之后就停止了
#前台运行容器并执行死循环命令。因为执行的命令是死循环,该命令会一直运行,容器一直不会停止。
 docker run -i centos:7 /bin/bash -c "while true;do echo $(date);sleep 2;done"

5. Continuously run the container created by docker run in the background

You need to add the -d option after the docker run command to make the Docker container run in the background as a daemon. And the program run by the container cannot end (commands in the container are still running in the foreground).

Using the -itd option, a container can be created and run continuously. Even if you use the exit command to exit the container after entering the container, the container will not be stopped.

#-itd 后台运行容器.使用--name 指定名称
 docker run -itd --name test2 centos:7  /bin/bash
 ​
 #查看容器状态,可以看出始终处于 UP,运行状态
 docker ps -a
 CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS         PORTS     NAMES
 baf5bdda3d28   centos:7   "/bin/bash"   10 seconds ago   Up 9 seconds             test2
#后台运行容器,并执行命令"tail -f /dev/null"。因为tail -f不会自动停止,会一直运行,所以容器不会退出。
 docker run -d --name test3 centos:7 tail -f /dev/null
 ​
 #查看容器状态,可以看出始终处于 UP,运行状态
 docker ps -a
 CONTAINER ID   IMAGE      COMMAND               CREATED         STATUS         PORTS     NAMES
 90d13d0606a9   centos:7   "tail -f /dev/null"   2 seconds ago   Up 1 second              test3
 a15ab19788a7   centos:7   "/bin/bash"           5 minutes ago   Up 5 minutes             test2

6. Stop the container

格式:
 docker stop 容器的ID/名称      #正常停止,可以给容器一个等待时间,以防止数据的丢失。
 或 
 docker kill 容器的ID/名称   #强制停止,(相当于linux命令 kill -9),不会给容器反应时间,可能造成数据丢失
 ​
 docker stop $(docker ps -aq)   #批量停止容器
 ​
 docker ps -a |awk 'NR>=2 {print $1}' | xargs docker stop   #批量停止容器

7. Delete the container

Containers in the running state cannot be deleted. You need to stop them first and then delete them. Or use -f to force delete.

格式:docker rm <容器ID/名称> [-f]   #删除容器
 ​
 docker rm $(docker ps -aq)         #批量删除所有容器
 ​
 docker ps -a |awk 'NR>=2 {print $1}' | xargs docker rm   #批量删除所有容器
#无法删除运行中的容器,需要先停止容器或强制删除
 [root@yuji ~]# docker rm 3a8ef7538a21
 Error response from daemon: You cannot remove a running container 3a8ef7538a21e917758bc700fa856af45de15a860fda4dc9b9a333b33ced568c. Stop the container before attempting removal or force remove
 ​
 #已停止的容器,可以直接删除
 [root@yuji ~]# docker rm baf5bdda3d28
 baf5bdda3d28
 ​
 #批量删除所有容器
 [root@yuji ~]# docker ps -aq                     #只显示所有容器的ID
 3a8ef7538a21
 a03185501236
 [root@yuji ~]# docker rm $(docker ps -aq) -f     #强制删除所有容器
 3a8ef7538a21
 a03185501236

8. Access to the container

When you need to enter the container for command operations, you can use the docker exec command to enter the running container. Before entering the container, make sure the container is running.

  • We usually use docker exec -it bash to enter the container and open a pseudo-terminal in the bash environment. You can also replace bash with another command, and let the container output the result to the screen after execution
  • It can be followed by the -c option "command sequence" . The command sequence can be multiple commands, separated by semicolons, and multiple commands can be executed at one time.

docker run -it will create a foreground process, but will terminate the process after entering exit.

docker attach will connect to the input and output stream in the container by connecting stdin, and will terminate the container process after entering exit.

docker exec -it will connect to the container, you can enter the container like SSH, perform operations, and you can exit the container through exit without affecting the running of the container.

 格式:docker exec -it 容器ID/名称 /bin/bash
 ​
 -i 选项表示让容器的输入保持打开。
 -t 选项表示让 Docker 分配一个伪终端。
 -it /bin/bash:进入容器时需要指定一个shell环境。
#前台运行容器时,可以同时指定shell环境直接进行容器,但exit退出容器后,容器就停止了。
 [root@yuji ~]# docker run -it --name yy1 centos:7 /bin/bash
 [root@c86f0e6b5433 /]# exit     #退出容器
 exit
 [root@yuji ~]# docker ps -a     #
 CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                     PORTS     NAMES
 c86f0e6b5433   centos:7   "/bin/bash"   16 seconds ago   Exited (0) 2 seconds ago   
docker run -itd --name yy2 centos:7    #后台运行容器
 docker exec -it yy2 /bin/bash          #使用exec进行容器,之后exit退出容器,容器不会停止
 ​
 [root@yuji ~]# docker run -itd --name yy2 centos:7
 f16a29a2942434cc8dd2b7b29cf56d3e6a5fba32caad329c1b59c7f1fa295685
 [root@yuji ~]# docker exec -it yy2 /bin/bash
 [root@f16a29a29424 /]# ls
 anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
 [root@f16a29a29424 /]# exit
 exit
 [root@yuji ~]# docker ps -a
 CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                     PORTS     NAMES
 f16a29a29424   centos:7   "/bin/bash"   23 seconds ago   Up 22 seconds                        yy2
 c86f0e6b5433   centos:7   "/bin/bash"   5 minutes ago    Exited (0) 5 minutes ago

9. View the metadata of the container - docker inspect

格式:docker inspect 容器id/容器名称
 ​
 docker ps -a                   #先查看运行时容器的id
 ​
 docker inspect 51fd134c7cc3    #查看容器的元数据
 "Gateway": "172.17.0.1",
             "GlobalIPv6Address": "",
             "GlobalIPv6PrefixLen": 0,
             "IPAddress": "172.17.0.2",       #容器的IP
             "IPPrefixLen": 16,
             "IPv6Gateway": "",
             "MacAddress": "02:42:ac:11:00:02",
             "Networks": {
                 "bridge": {                 #使用的是网桥模式

 

10. Container logs

格式:
 docker logs 容器ID/容器名
 #后台启动容器,并使用-P随机映射一个端口
 [root@yuji ~]# docker run -id -P nginx:latest
 93007aee7e8f2d74345408191d827d65bcc2e204b53244a3ab9fc3d8f8f6079c
 [root@yuji ~]# docker ps -a       #映射端口为49153
 CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS                                     NAMES
 93007aee7e8f   nginx:latest   "/docker-entrypoint.…"   2 seconds ago    Up 1 second                 0.0.0.0:49153->80/tcp, :::49153->80/tcp   practical_m               clean
 51fd134c7cc3   centos:7       "/bin/bash"              13 minutes ago   Up 13 minutes                                                         yy2
 799d2062f4dc   centos:7       "/bin/bash"              13 minutes ago   Exited (0) 13 minutes ago 
 ​
 #浏览器使用宿主机IP和映射端口号,访问容器
 ​
 #查看容器日志
 [root@yuji ~]# docker logs 93007aee7e8f
 192.168.41.1 - - [26/Jun/2022:14:26:51 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36" "-"
 2022/06/26 14:26:51 [error] 31#31: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.41.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.41.46:49153", referrer: "http://192.168.41.46:49153/"
 192.168.41.1 - - [26/Jun/2022:14:26:51 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.41.46:49153/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36" "-"

11. File replication between host and container

1) Copy the files in the host to the container

#将宿主机中的文件复制到容器中
 echo 123 > /opt/f1.txt
 docker cp /opt/f1.txt  容器ID:/tmp/
 ​
 [root@yuji ~]# echo 123 > /opt/f1.txt
 [root@yuji ~]# docker cp /opt/f1.txt  93007aee7e8f:/tmp/    #将宿主机中的文件复制到容器中
 [root@yuji ~]# docker exec -it 93007aee7e8f /bin/bash       #进入容器查看是否复制成功
 root@93007aee7e8f:/# cd /tmp
 root@93007aee7e8f:/tmp# ls
 f1.txt 
 root@93007aee7e8f:/tmp# cat f1.txt      #复制成功
 123
 root@93007aee7e8f:/tmp#

2) Copy the files in the container to the host

docker cp 容器ID:/tmp/f1.txt  ~/abc.txt   #复制并重命名
 ​
 #将容器中/tmp目录下的f1.txt文件,复制到宿主机的/root目录下,并重命名为abc.txt
 [root@yuji ~]# docker cp 93007aee7e8f:/tmp/f1.txt  ~/abc.txt  
 [root@yuji ~]# ls
 abc.txt  anaconda-ks.cfg  initial-setup-ks.cfg  mynginx.tar  公共  模板  视频  图片  文档  下载  音乐  桌面

12. Container export and import (container migration)

Users can migrate any Docker container from one machine to another. During the migration process, you can use the docker export command to export the created container as a file, regardless of whether the container is running or stopped.

The export file can be transferred to other machines, and the migration of the container can be realized through the corresponding import command. (Note: The image will be generated after importing the file, but the container will not be created automatically.)

#导出格式:
 docker export 容器ID/名称 > 导出文件名
 ​
 #导入格式:
 cat 导出文件名 | docker import – 镜像名称:标签
 #主机A将容器导出为文件,并将导出的文件传给主机B
 [root@yuji ~]# docker export 51fd134c7cc3 > mycentos7
 [root@yuji ~]# scp ~/mycentos7 192.168.41.45:/root/
 ​
 #主机B将文件导入生成镜像,并使用导入的镜像创建容器
 [root@node02 ~]# cat mycentos7 | docker import - centos7:test   #导入后会生成镜像,但不会创建容器
 [root@node02 ~]# docker run -id centos7:test                     #使用导入的镜像创建容器

Summary of docker container commands:

Guess you like

Origin blog.csdn.net/shitianyu6/article/details/127958410