Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cleanup docker containers and images on linux machines

We have Linux redhat machine with docker and docker compose

Now we want to clean all containers and images - like we have scratch new docker

As I understand to get that , we need to perform the following procedure with this order:

Am I right with this procedure? , or I missing something?

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

example

first find - CONTAINER 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)

stop container

docker stop 654fa81f4439
654fa81f4439

2)

delete container

docker container rm 654fa81f4439
654fa81f4439

3)

find image ID

docker images
REPOSITORY                                  TAG                 IMAGE ID            CREATED             SIZE

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

delete image

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
like image 656
jessica Avatar asked Dec 09 '25 12:12

jessica


1 Answers

In short, yes, it is the correct procedure to clear all the containers and images.

But you can do it more easily. For example:

  • Stop all containers at once: docker container stop $(docker container ls -aq)
  • Remove all stopped containers: docker container prune --force
  • Remove all unnused images: docker image prune --all --force

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


Others Docker resources may be left on your system (such as networks and build caches).

You can appeal to docker system prune to 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
like image 105
Eduardo Baitello Avatar answered Dec 12 '25 16:12

Eduardo Baitello