8

Sometimes I just want to clear everything out of docker, and start with a clean slate. Is there a systematic way to just remove all my Docker containers & images?

Containers

$ docker ps -a | head
CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS                     PORTS               NAMES
7b055e9e5f1f        fedora:latest        /bin/sh                6 days ago          Exited (0) 6 days ago                          ecstatic_colden        
40da968258eb        fedora:latest        /bin/sh                6 days ago          Exited (127) 6 days ago                        naughty_thompson       
5be4581afad6        fedora:latest        /bin/bash              6 days ago          Exited (0) 6 days ago                          kickass_wright         
4d6c33d4be6d        fedora:latest        /bin/bash              6 days ago          Exited (0) 6 days ago                          furious_fermat         
db7a29bdbc2d        fedora:latest        /bin/bash              6 days ago          Exited (0) 6 days ago                          mad_hawking            
e6b7365690ce        fedora:latest        /bin/bash              6 days ago          Exited (0) 6 days ago                          trusting_ardinghelli   
15655c21fcd3        8018e08e6a58         /bin/sh -c 'apt-get    3 weeks ago         Exited (100) 3 weeks ago                       drunk_bardeen          
607547aabbca        f02ed0c206d5         /bin/sh -c 'apt-get    3 weeks ago         Exited (100) 3 weeks ago                       cocky_franklin         
f031f28bd29a        f02ed0c206d5         /bin/sh -c 'apt-get    3 weeks ago         Exited (100) 3 weeks ago                       stoic_mcclintock      

Images

$ docker images -a | head
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker              master              266a5746d179        2 weeks ago         1.528 GB
<none>              <none>              c1f6a4b1345c        3 weeks ago         1.526 GB
<none>              <none>              6558c36ecb3f        3 weeks ago         1.526 GB
<none>              <none>              b1b7c7a6b6a7        3 weeks ago         1.24 GB
<none>              <none>              0bf9d6bf97e9        3 weeks ago         1.182 GB
<none>              <none>              48c444f1d2e7        3 weeks ago         1.182 GB
<none>              <none>              5c5c172a0038        3 weeks ago         1.182 GB
<none>              <none>              de5e09bb86c1        3 weeks ago         1.182 GB
<none>              <none>              785aaf265f18        3 weeks ago         1.182 GB
9

The simplest way to do this is to use do the following 3 steps:

  1. stop all the containers
  2. remove all the containers
  3. remove all the images

To achieve this you can make use of the docker ps and docker images commands abilities to just return you a list of IDs via there -q or --quiet switch. This gets rid of all the cruft that both these commands typically returns and just gives you a nice list, like so:

Default output
$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS                     PORTS               NAMES
7b055e9e5f1f        fedora:latest        /bin/sh                6 days ago          Exited (0) 6 days ago                          ecstatic_colden        
40da968258eb        fedora:latest        /bin/sh                6 days ago          Exited (127) 6 days ago                        naughty_thompson      
Quiet output
$ docker ps -aq
7b055e9e5f1f
40da968258eb
5be4581afad6
4d6c33d4be6d

NOTE: Both docker images and docker ps also make use of the -a or --all switch which shows you all the IDs.

You can utilize this output to construct Docker commands that make use of output from other Docker commands like so:

$ docker rm $(docker ps -qa)

Cleaning house

So to accomplish your task of removing everything you'd use the following commands:

$ docker stop $(docker ps -qa)
$ docker rm $(docker ps -qa)
$ docker rmi $(docker images -qa)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.