Docker: Remove all images and containers

Problem:

You use Docker, but working with it created lots of images and containers. You want to remove all of them to save disk space.

Solution

Warning: This will destroy all your images and containers. There is no way to restore them!

Run those commands in a shell:

docker rm $(docker ps -a -q)
docker rmi $(docker images -q)

This solution has be proposed by GitHub user @crosbymichael in this issue

In case you want to delete even those images that are referenced in repositories, use

docker rmi $(docker images -q) --force

Background information:

You can see the containers on your computer using

docker container ls

Example output:

CONTAINER ID        IMAGE                                                     COMMAND                  CREATED             STATUS              PORTS                              NAMES
3477a4dcdce2        docker.elastic.co/elasticsearch/elasticsearch-oss:6.7.1   "/usr/local/bin/dock…"   2 days ago          Up 2 days           0.0.0.0:9200->9200/tcp, 9300/tcp   elasticsearch1
67997f002f15        appbaseio/dejavu                                          "http-server '-p 135…"   2 days ago          Up 2 days           0.0.0.0:1358->1358/tcp             dejavu

As we can see, two containers are currently running on this computer.

For each container, the IMAGE column shows us which image the container is running. For example, the second container is running on the appbaseio/dejavu image.

When we tell docker to show us all images currently listed on the computer:

$ docker image ls -a
REPOSITORY                                          TAG                 IMAGE ID            CREATED             SIZE
<none>                                              <none>              39ac507bd271        11 days ago         962MB
<none>                                              <none>              6a2c829a5d4f        11 days ago         962MB
docker.elastic.co/elasticsearch/elasticsearch-oss   6.7.1               c91b419ac445        3 weeks ago         682MB
pypy                                                3                   75e018538e96        4 weeks ago         962MB
node                                                10                  64c810caf95a        4 weeks ago         899MB
appbaseio/dejavu                                    latest              47b8375dc541        2 months ago        141MB
risingstack/alpine                                  3.4-v4.4.4-3.6.1    2d384efd00ab        2 years ago         247MB

We can immediately see that there are way more images than containers

There are four main reasons for that:

In order to delete an image that you no longer need, use the docker image rm <image ID> command. Copy the image ID from the IMAGE ID column of the output of docker image ls as shown above.

For example, in order to delete the version 7.0.0 image of elasticsearch, use

$ docker image rm 0e92b9e5fe1e
Untagged: docker.elastic.co/elasticsearch/elasticsearch-oss:7.0.0
Untagged: docker.elastic.co/elasticsearch/elasticsearch-oss@sha256:e9ab1f4c2ffbc7054dab91e8dc7fd9a56d53cce734098910d876eb14849b1ba3
Deleted: sha256:0e92b9e5fe1e0949f5174ed86d9cbc3c2a041df493453f82b6c044ff78c7d17b
Deleted: sha256:c2a552806b86aba72a6e6ea5efaaa295286e8b0cf42a14d799428e58f20e7090
Deleted: sha256:71e7a706f7c03f0c7add24844f7b311edf83838ef383a877bcb94684b011893d
Deleted: sha256:d43e5d351c26273e43b11f6937cb9a3192d856b4e083acfbc14b8500056221f2
Deleted: sha256:f3a2ae77ca96f2f55300b00beedbf14ade0a27bf29c395dcbf84b5bf02ef80ad
Deleted: sha256:5d7f2d83e59c0684b4a94ab5f6479d798444bdca8fe6c813f7ef6188cdd12ce5
Deleted: sha256:cc16decd0c6f218c3a21749e747d43deced9e8929e8db0b6050dd4102301651a

As you can see from the output, this will not only delete the ElasticSearch image 0e92b9e5fe1e itself but also all the other images that image depended on (which are not also used by any other image).