docker tutorials

How to Remove All Old Stopped Docker Containers ?

This tutorial will guide you on how to remove all old stopped docker containers. You need to remove or cleanup docker containers which are not running and when you don’t want to do anything with that. Let’s see how to remove docker containers effectively.

Remove all old stopped docker containers

There are some data management docker commands which will help us to visualize how much space the docker images, containers, volumes etc., are consuming on disk. The following command will provide summary about different docker objects.

$ docker system df

TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              5                   2                   80.68MB             79.45MB (98%)
Containers          3                   0                   147.8MB             147.8MB (100%)
Local Volumes       1                   1                   0B                  0B
Build Cache         0                   0                   0B                  0B

Use the following docker management command to check the space usage details.

$ docker system df -v

Images space usage:

REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE                SHARED SIZE         UNIQUE SIZE         CONTAINERS
<none>                                  <none>              c996d73e5113        13 minutes ago      73.86MB             73.86MB             0B                  0
ubuntuv1                                latest              8f1442d03996        24 minutes ago      73.86MB             73.86MB             0B                  0
busybox                                 latest              018c9d7b792b        2 days ago          1.224MB             0B                  1.224MB             1
ubuntu                                  latest              1e4467b07108        5 days ago          73.86MB             73.86MB             0B                  2
gcr.io/sneppets-gcp/hellodocker-image   latest              40041a9ff2e2        4 months ago        5.596MB             0B                  5.596MB             0

Containers space usage:

CONTAINER ID        IMAGE               COMMAND                  LOCAL VOLUMES       SIZE                CREATED             STATUS                      NAMES
ce8ed5705333        ubuntu              "/bin/bash -c 'apt-g…"   0                   148MB               27 minutes ago      Exited (1) 27 minutes ago   hungry_jones
5abcfb446878        busybox             "ls /world"              1                   0B                  28 minutes ago      Exited (0) 28 minutes ago   gallant_ellis
d41a74c34984        ubuntu              "bash"                   0                   0B                  29 minutes ago      Exited (0) 29 minutes ago   priceless_benz

Local Volumes space usage:

VOLUME NAME         LINKS               SIZE
hello               1                   0B

Build cache usage: 0B

CACHE ID            CACHE TYPE          SIZE                CREATED             LAST USED           USAGE               SHARED

You can cross also check once with following docker command to see list of docker containers which are exited and not running.

 

$ docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
76830f8b0e95        busybox             "ls /world"              23 minutes ago      Exited (0) 23 minutes ago                       friendly_booth
070b4fd9217d        ubuntu              "/bin/bash -c 'apt-g…"   24 minutes ago      Exited (1) 23 minutes ago                       pedantic_wu
b587eb018968        ubuntu              "bash"                   24 minutes ago      Exited (0) 24 minutes ago                       mystifying_sutherland

From the above response, we could see that none of the docker containers are running. All are exited.

To remove all old stopped or exited container run the following command.

$ docker container prune

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
76830f8b0e9512e849a2410ee9aadaa06b665019921a60bf9a9b2165bb691058
070b4fd9217dcd07e253a4a887ebd0bdfb466b61c259e71683de30aaefe2003a
b587eb018968f8f52c2dc049f3f244ba0b49a185c0227de60303de5f3ad6b2db

Total reclaimed space: 147.8MB

Now, run docker ps  command to verify that all the exited or stopped containers are removed.

$ docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

That’s it all the non-running containers are removed.

To delete all unused data in order (stopped containers, networks not used , dangling image etc.,) use the following command.

$ docker system prune -a

WARNING! This will remove:
  - all stopped containers
  - all networks 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
Deleted Containers:
ce8ed57053339454c8870adc807dad9b9e7db6a97af0ddac852672d713be1c7f
5abcfb446878d16f8bbc82037c5505c22f1ca45d6c48907c1ab6caa0e53424a7
d41a74c34984b7c056657723d55ed66543019cf11dbeca603eeb669586a1d1b0

Deleted Images:
deleted: sha256:4e6d127b5544c1d9922704683f6e4458a4f10db85be732c33895adb8253d09a0
untagged: busybox:latest
untagged: busybox@sha256:4f47c01fa91355af2865ac10fef5bf6ec9c7f42ad2321377c21e844427972977
deleted: sha256:018c9d7b792b4be80095d957533667279843acf9a46c973067c8d1dff31ea8b4
deleted: sha256:514c3a3e64d4ebf15f482c9e8909d130bcd53bcc452f0225b0a04744de7b8c43
untagged: ubuntu:latest
untagged: ubuntu@sha256:5d1d5407f353843ecf8b16524bc5565aa332e9e6a1297c73a92d3e754b8a636d
untagged: gcr.io/sneppets-gcp/hellodocker-image:latest
untagged: gcr.io/sneppets-gcp/hellodocker-image@sha256:20da7a3d10a6cdbc541a1f582530907b13a98429e594fa8b56e0236cb187a0e1
deleted: sha256:40041a9ff2e2d5a5e2ed368b09a94ff20f55208c52d072fdc74726060f8219a7
deleted: sha256:2731f56a29a1666251c705101281c6b46700989018f64c76183fcce1086c9fc4
deleted: sha256:beee9f30bc1f711043e78d4a2be0668955d4b761d587d6f60c2c8dc081efb203
untagged: ubuntuv1:latest
deleted: sha256:f5914386e10761361857beb08a00e5b20df6b4828cc31278ec85a3a0db0058eb
deleted: sha256:1e4467b07108685c38297025797890f0492c4ec509212e2e4b4822d367fe6bc8
deleted: sha256:7515ee845913c8df9826c988341a09e0240e291c66bdc436a067e070d7910a1f
deleted: sha256:50ebe6a0675f1ed7ca499a2ec7d8cc993d495dd66ca1035c218ec5efcb6fbb8c
deleted: sha256:2515e0ecfb82d58c004c4b53fcf9230d9eca8d0f5f823c20172be01eec587ccb
deleted: sha256:ce30112909569cead47eac188789d0cf95924b166405aa4b71fb500d6e4ae08d

Total reclaimed space: 228.4MB

Docker System Prune Command Options

To remove all unused or stopped containers, networks, images (both dangling and unreferenced), and optionally, volumes use the following options in docker system prune [OPTIONS]

--all , -a Remove all unused images not just dangling ones
--filter Provide filter values (e.g. ‘label==’)
--force , -f Do not prompt for confirmation
--volumes Prune volumes

Hope this helped 🙂

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments