在docker 下删除一个或多个image及container

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hxj2011111109/article/details/52475670


首先以root用户登陆


删除一个或多个container

A.删除一个container 命令格式:docker ps -a 查看container ID

    docker rm container_name/ID

root@ubuntu-daisy:~# docker  ps  -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1bd86f625ee9        hello-world         "/hello"            37 seconds ago      Exited (0) 35 seconds ago                       admiring_colden


root@ubuntu-daisy:~# docker  rm  1bd86f625ee9
1bd86f625ee9
root@ubuntu-daisy:~# docker  ps  -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES


B.删除多个container

eg 同时运行了hello-world两次,若要一次性删除hello-world的container,使用命令查看:

   docker ps –a –f ancestor=hello-world 或

   dockerps –s –filter=”ancestor=hello-world”

然后使用 docker rm $(  docker ps –a –f ancestor=hello-world )就删除了所有的hello-world container


root@ubuntu-daisy:~# docker  ps  -a  -f   ancestor=hello-world

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES

0da0793ec7b9       hello-world        "/hello"            58seconds ago      Exited (0) 56 secondsago                       sick_lovelace

ee989cf4fdfa       hello-world        "/hello"            4hours ago         Exited (0) 4 hoursago                          high_bhabha


或者  root@ubuntu-daisy:~# docker ps -a--filter="ancestor=hello-world"

CONTAINER ID       IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES

0da0793ec7b9       hello-world        "/hello"           About a minute ago   Exited (0) Abouta minute ago                      sick_lovelace

ee989cf4fdfa        hello-world         "/hello"            4 hours ago          Exited (0) 4 hours ago                              high_bhabha


root@ubuntu-daisy:~# docker rm $( docker ps -a -f  ancestor=hello-world )

29517fba49bb

f813458b27af

root@ubuntu-daisy:~# docker ps  -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES

删除所有的container,命令格式:docker rm  $( docker ps -a -q )


删除一个或多个image

删除image前需要stop 所有的container

root@ubuntu-daisy:~# docker stop $(docker  ps  -a -q)

094dfd5badc0

68f1f41a2e74

查看所有的image

root@ubuntu-daisy:~# docker images

REPOSITORY         TAG                 IMAGE ID            CREATED             SIZE

hello-world        latest             c54a2cc56cbb        9 weeksago         1.848 kB

删除一个 hello-world image ,命令格式:docker  rmi   image_name/ID

root@ubuntu-daisy:~# docker rmi c54a2cc56

Untagged: hello-world:latest

Untagged:hello-world@sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9

Deleted:sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc

Deleted:sha256:a02596fdd012f22b03af6ad7d11fa590c57507558357b079c3e8cebceb4262d7

root@ubuntu-daisy:~# docker images

REPOSITORY         TAG                 IMAGE ID            CREATED             SIZE

此时已经删除hello-world  image


若要删除所有的image, 使用命令:docker rmi  $( docker  images -q )

有时image删不掉,可以强制删除:docker rmi  imagename 或者docker  rmi  -f   imagename












展开阅读全文

没有更多推荐了,返回首页