Docker cleanup commands

Docker cleanup commands

It is important that from time to time, machines running Docker are cleaned because otherwise, Docker leftovers will start piling up and eating precious storage space.


To get a list of currently stopped containers, run:

$ docker container ls -a --filter status=exited --filter status=created

If everything looks good and all the containers listed are not necessary anymore, you can run the following command to remove them:

$ docker container prune


It is also possible to specify filters with the until and label keywords:

$ docker container prune --filter "until=48h"

This will remove all images created more than 48 hours ago, according to their timestamp relative to the current time.


List all Docker images:

$ docker image ls
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
jekyll/builder                latest              111bf2f57942        14 hours ago        561MB
jekyll/builder                <none>              56e0f8a07d54        38 hours ago        561MB
jekyll/builder                <none>              ddaac7a96770        2 days ago          561MB
jekyll/builder                <none>              9de64f166ee4        4 days ago          562MB
gitlab/gitlab-ce              latest              a4e39f0caf21        5 days ago          1.75GB
jekyll/builder                <none>              cbdb93edfaa7        6 days ago          562MB
jekyll/builder                <none>              e9101988ac31        7 days ago          562MB
jekyll/builder                <none>              80061ade1c9d        9 days ago          562MB
gitlab/gitlab-ce              <none>              f3df0ca02243        9 days ago          1.75GB
jekyll/builder                <none>              fcee4caf1c81        10 days ago         562MB
jekyll/builder                <none>              40a9987907ac        11 days ago         477MB
jekyll/builder                <none>              7626c80c2d4c        12 days ago         477MB
gitlab/gitlab-runner-helper   x86_64-a987417a     6eb5de6b5081        13 days ago         52.4MB

It is possible to remove images by specifying their ID. For example, this command will remove both gitlab/gitlab-ce images:

$ docker image rm a4e39f0caf21 f3df0ca02243

It is possible to prune unused images automatically:

$ docker image prune


List all volumes by running:

$ docker volume ls

Then, you can remove volumes that are not in use:

$ docker volume prune

Prune everything

This will delete anything that is not in use anymore. Be careful, as this command will delete stopped containers as well as their volumes and images. Only run this if all necessary containers are running.

$ docker system prune --volumes -a

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 dangling images
        - all build cache
Are you sure you want to continue? [y/N]

To also skip confirmation, use:

$ docker system prune --volumes -af

Wrapping up

The prune or ls keywords can be used after container, network, volume or image which will respectively remove unused or list the present objects on the host.

$ docker container ls
$ docker container prune
$ docker network ls
$ docker network prune
$ docker volume ls
$ docker volume prune
$ docker image ls
$ docker image prune