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

时间:2019-11-07 19:33:55

标签: docker docker-compose containers redhat docker-machine

我们有带docker和docker compose的Linux redhat机器

现在,我们要清理所有容器和图像-就像我们有新的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

1 个答案:

答案 0 :(得分:5)

总之,是的,清除所有容器和图像是正确的过程。

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

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

有时Docker volumes用于容器保存数据。您可能需要clean them toodocker 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