如何在Linux机器上清理Docker容器和镜像

3
我们有一台安装了docker和docker compose的Linux redhat机器。
现在我们想要清除所有容器和镜像——就像我们拥有一个全新的docker一样。
据我所知,为了达到这个目的,我们需要按照以下顺序执行以下步骤:
1. 停止并删除所有正在运行的容器。 2. 删除所有本地镜像。 3. 使用命令 "docker system prune" 清理所有未使用的资源。
我的操作流程正确吗?还是我漏掉了什么步骤?
 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
1个回答

8

简单来说,是的,清除所有容器和镜像是正确的操作。

但您也可以更轻松地完成它。例如:

有时容器会使用Docker卷来保存数据。您可能还想清理它们 (docker volume prune --force)。


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

您可以使用 docker system prune 命令来删除所有未使用的数据

$ 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接