如何在 linux 台机器上清理 docker 个容器和镜像

How to cleanup docker containers and images on linux machines

我们有 Linux redhat 机器 docker 和 docker 组成

现在我们要清理所有容器和图像 - 就像我们从头开始一样 docker

据我了解,我们需要使用此命令执行以下过程:

我对这个程序是否正确? ,或者我遗漏了什么?

 1. docker stop <CONTAINER ID>
 2. docker container rm <CONTAINER ID>
 3. docker image rm  <IMAGE ID>

例子

首先找到 - 容器 ID

docker ps
CONTAINER ID        IMAGE                                             COMMAND                  CREATED             STATUS              PORTS                                        NAMES
654fa81f4439        confluentinc/cp-enterprise-control-center:5.0.0   "/etc/confluent/dock…"   9 minutes ago       Up 9 minutes        0.0.0.0:9021->9021/tcp                       control-center

1)

停止容器

docker stop 654fa81f4439
654fa81f4439

2)

删除容器

docker container rm 654fa81f4439
654fa81f4439

3)

查找图片 ID

docker images
REPOSITORY                                  TAG                 IMAGE ID            CREATED             SIZE

confluentinc/cp-enterprise-control-center   5.0.0               e0bd9a5edb95        15 months ago       617MB

删除图片

ocker image rm  e0bd9a5edb95
Untagged: confluentinc/cp-enterprise-control-center:5.0.0
Untagged: confluentinc/cp-enterprise-control-center@sha256:2e406ff8c6b1b8be6bf01ccdf68b14be0f0759db27c050dddce4b02ee0894127
Deleted: sha256:e0bd9a5edb9510a326934fa1a80a4875ab981c5007354de28f53bfb3e11bc34a
Deleted: sha256:c23255297f6d75f156baf963786d3ded1d045b726d74ed59c258dc8209bac078
Deleted: sha256:6cab492e72ca2578897b7ceecb196e728671158e262957f3c01e53fd42f6f8b4

总之,是的,清除所有容器和镜像才是正确的步骤。

但你可以更轻松地做到这一点。例如:

  • 停止所有容器 在 一次: docker container stop $(docker container ls -aq)
  • 删除 all stopped 个容器:docker container prune --force
  • 删除 all unnused 张图片:docker image prune --all --force

有时 Docker volumes are used for containers to persist data. You may want to clean them too (docker volume prune --force).


其他 Docker 资源可能留在您的系统上(例如网络和构建缓存)。

您可以向docker system prune申诉到remove all unused data

$ docker system prune --all --volumes
WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all images without at least one container associated to them
        - all build cache
Are you sure you want to continue? [y/N] y