Docker rm命令

本文将介绍 docker rm命令,该命令用来删除容器,而“docker rmi”删除镜像(i为image)。

Docker rm命令用来删除容器,而“docker rmi”删除镜像(i为image)。docker rm命令的帮助信息:

[root@localhost ~]# docker rm --help

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
  -l, --link      Remove the specified link
  -v, --volumes   Remove the volumes associated with the container

其中:

  • -f, --force 强制删除一个正在运行的容器(使用 SIGKILL)

  • -l, --link 删除指定的连接

  • -v, --volumes 删除与容器关联的卷

实例:使用容器ID删除已经停止的容器,如下:

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
5a97a6b111af        memcached:1.4.21    "memcached"         4 minutes ago       Exited (137) 3 minutes ago                        quirky_bhabha

[root@localhost ~]# docker rm 5a97a6b111af
5a97a6b111af

上面,我们使用“docker ps -a”查看所有容器。然后使用“docker rm 5a97a6b111af”删除容器,该容器的状态为“Exited (137) 3 minutes ago”表示已停止。

如果我们删除一个正在运行的容器呢?

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bf73eeb136ce        ubuntu              "/bin/bash"         4 seconds ago       Up 3 seconds                            gallant_turing

[root@localhost ~]# docker rm bf73eeb136ce
Error response from daemon: You cannot remove a running container bf73eeb136ce1c76dd836bcd1df20cd642d39140c07151c797de81914970ba6c. Stop the container before attempting removal or force remove

但是抛出了错误“You cannot remove a running container...”,大概意思为“不能删除正在运行的容器”。我们可使用“-f”选项强制删除容器,如下:

[root@localhost ~]# docker rm -f bf73eeb136ce
bf73eeb136ce

问题来了,怎样批量删除容器呢?

批量删除容器共有两种方式:

(1)方式一:使用“docker rm -f $(docker ps -aq)”,如下:

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
5c27f0d18a73        centos              "/bin/bash"         8 minutes ago       Exited (137) 7 minutes ago                        festive_keldysh
af9ba2baa42e        centos              "/bin/bash"         10 minutes ago      Exited (137) 9 minutes ago                        musing_cerf
b60d1784c305        ubuntu              "/bin/bash"         10 minutes ago      Exited (137) 7 minutes ago                        naughty_wozniak
ece33456afe1        centos              "/bin/bash"         11 minutes ago      Exited (137) 11 minutes ago                       sharp_hopper
574ad33d0772        ubuntu              "/bin/bash"         11 minutes ago      Exited (137) 11 minutes ago                       blissful_jang
ddfd18831621        centos              "/bin/bash"         22 minutes ago      Exited (137) 12 minutes ago                       gifted_lovelace
c100b8e45fe8        ubuntu              "/bin/bash"         22 minutes ago      Exited (0) 15 minutes ago                         goofy_jepsen
9ee966dd7f83        ubuntu              "/bin/bash"         31 minutes ago      Exited (0) 31 minutes ago                         goofy_shannon
035d9d7e276e        centos              "/bin/bash"         32 minutes ago      Exited (137) 24 minutes ago                       kind_bohr

[root@localhost ~]# docker rm -f $(docker ps -aq)
5c27f0d18a73
af9ba2baa42e
b60d1784c305
ece33456afe1
574ad33d0772
ddfd18831621
c100b8e45fe8
9ee966dd7f83
035d9d7e276e

(2)方式二:使用“docker ps -aq | xargs docker rm -f”,如下:

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
16d0474f9abb        memcached:1.4.21    "memcached"         7 seconds ago       Up 7 seconds        11211/tcp           vibrant_hellman
dc38deba0bb3        centos              "/bin/bash"         10 seconds ago      Up 9 seconds                            inspiring_johnson
90c97e8ade06        ubuntu              "/bin/bash"         13 seconds ago      Up 12 seconds                           hungry_nash

[root@localhost ~]# docker ps -aq | xargs docker rm -f
16d0474f9abb
dc38deba0bb3
90c97e8ade06
天赋如同自然花木,要用学习来修剪。 —— 培根
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号