Docker 04 容器命令

查看

docker ps

语法:

docker ps [参数]

参数:

  • -a:查看所有容器(包括正在运行的和已经停止的)。
  • -n:显示最近创建的容器,设置显示个数。
  • -q:只显示容器的编号。

查看正在运行的容器:

[root@sail ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
1aaf76d85b9e   centos    "/bin/bash"   About a minute ago   Up About a minute             intelligent_proskuriakova

查看所有容器:

[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED              STATUS                      PORTS     NAMES
1aaf76d85b9e   centos         "/bin/bash"   About a minute ago   Up About a minute                     intelligent_proskuriakova
7ac04abd5a1f   centos         "/bin/bash"   3 minutes ago        Exited (0) 3 minutes ago              elegant_euler
bbb87116d848   centos         "/bin/bash"   8 minutes ago        Exited (0) 3 minutes ago              focused_boyd
81c83ea42dc0   centos         "/bin/bash"   28 minutes ago       Exited (0) 19 minutes ago             zealous_proskuriakova
52918b3ce8f6   feb5d9fea6a5   "/hello"      11 days ago          Exited (0) 11 days ago                friendly_ramanujan

显示最近创建的 2 个容器:

[root@sail ~]# docker ps -a -n=2
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS                   PORTS     NAMES
1aaf76d85b9e   centos    "/bin/bash"   5 hours ago   Up 5 hours                         intelligent_proskuriakova
7ac04abd5a1f   centos    "/bin/bash"   5 hours ago   Exited (0) 5 hours ago             elegant_euler

只显示容器的 ID:

[root@sail ~]# docker ps -aq
1aaf76d85b9e
7ac04abd5a1f
bbb87116d848
81c83ea42dc0
52918b3ce8f6

退出

exit

进入容器后,可以使用 exit 退出:

[root@sail ~]# docker run -it centos /bin/bash
[root@7ac04abd5a1f /]# exit
exit
[root@sail ~]# 
[root@sail ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

由此可见,这样退出后容器也会停止。

Ctrl + P + Q

如果想退出后容器不停止,可以使用 Ctrl + P + Q 快捷键退出:

[root@sail ~]# docker run -it centos /bin/bash
[root@1aaf76d85b9e /]# [root@sail ~]# docker ps # 此时即为使用 Ctrl + P + Q 快捷键的效果
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
1aaf76d85b9e   centos    "/bin/bash"   8 seconds ago   Up 8 seconds             intelligent_proskuriakova

删除

docker rm

语法:

docker rm [参数] 容器 [容器...]

参数:

  • -f:强制删除。

删除指定容器(不能删除正在运行的容器):

[root@sail ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS       PORTS     NAMES
1aaf76d85b9e   centos    "/bin/bash"   5 hours ago   Up 5 hours             intelligent_proskuriakova
[root@sail ~]# docker rm 1aaf76d85b9e
Error response from daemon: You cannot remove a running container 1aaf76d85b9ee5002411c1ea390fca05819f19dc400e85127731d37455cb0acc. Stop the container before attempting removal or force remove

强制删除指定容器:

[root@sail ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS       PORTS     NAMES
1aaf76d85b9e   centos    "/bin/bash"   5 hours ago   Up 5 hours             intelligent_proskuriakova
[root@sail ~]# docker rm -f 1aaf76d85b9e
1aaf76d85b9e

删除所有容器(先使用 docker ps -aq 获取所有容器的 ID,再调用 docker rm -f 递归删除):

[root@sail ~]# docker rm -f $(docker ps -aq)
7ac04abd5a1f
bbb87116d848
81c83ea42dc0
52918b3ce8f6
[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

删除所有容器(使用管道符 | 获取 Docker 相关的所有容器 ID 并使用 docker rm -f 删除):

[root@sail ~]# docker ps -a -q|xargs docker rm -f
2e61c4578eac
0ebe32ddfa50

启动

docker start

查看所有的容器,容器状态为关闭

[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                     PORTS     NAMES
569026bc0955   centos    "/bin/bash"   33 seconds ago   Exited (0) 7 seconds ago             centos03

运行关闭的容器:

[root@sail ~]# docker start 569026bc0955 
569026bc0955

再次查看所有的容器,容器状态为运行

[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
569026bc0955   centos    "/bin/bash"   About a minute ago   Up 1 second                   centos03

停止

docker stop

查看所有的容器,容器状态为运行

[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
569026bc0955   centos    "/bin/bash"   About a minute ago   Up 1 second                   centos03

关闭运行的容器:

[root@sail ~]# docker stop 569026bc0955
569026bc0955

再次查看所有的容器,容器状态为关闭

[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                      PORTS     NAMES
569026bc0955   centos    "/bin/bash"   4 minutes ago   Exited (0) 38 seconds ago             centos03

重启

docker restart

查看所有的容器,容器状态为关闭

[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                      PORTS     NAMES
569026bc0955   centos    "/bin/bash"   4 minutes ago   Exited (0) 38 seconds ago             centos03

重启关闭的容器:

[root@sail ~]# docker restart 569026bc0955
569026bc0955

再次查看所有的容器,容器状态为运行

[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS          PORTS     NAMES
569026bc0955   centos    "/bin/bash"   6 minutes ago   Up 20 seconds             centos03

杀掉

docker kill

查看所有的容器,容器状态为运行

[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS          PORTS     NAMES
569026bc0955   centos    "/bin/bash"   6 minutes ago   Up 20 seconds             centos03

杀掉运行的容器:

[root@sail ~]# docker kill 569026bc0955
569026bc0955

再次查看所有的容器,容器状态为关闭

[root@sail ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                        PORTS     NAMES
569026bc0955   centos    "/bin/bash"   8 minutes ago   Exited (137) 15 seconds ago             centos03

导出

docker export 目标容器 > 文件名称.tar

导入

docker import - 镜像名称 < 文件名称.tar

参考

https://www.bilibili.com/video/BV1og4y1q7M4?spm_id_from=333.999.0.0

https://www.bilibili.com/video/BV1kv411q7Qc?spm_id_from=333.999.0.0

版本

CentOS 7.6

Docker 20.10.11

posted @ 2021-12-09 21:11  天航星  阅读(109)  评论(0编辑  收藏  举报