Able Seaman: Keeping your Docker System Clean

After reading through many tutorials, and testing different features of Docker you may have some containers and images left in unexpected states.

We've compiled a list of useful commands so you can look into your docker system and clean things up if necessary.

Containers

List Containers:

$ docker ps # lists running containers
$ docker ps -a # list all containers

Stop Containers:

$ docker stop XXX # first few chars of container id
$ docker stop $(docker ps -a)

Remove Containers:

$ docker rm XXX # first few characters of container id
$ docker rm $(docker ps -aq) # remove all containers
$ docker rm -v $(docker ps -aq) # remove all containers and their volumes

The -q flag displays only the numeric id

Images

List Images

$ docker images # default hides intermediates
$ docker image ls -a # lists all images, including intermediates

Remove Images:

$ docker image rm XXX # first few chars of image id
$ docker rmi XXX # shortform
$ docker rmi $(docker images -q) # remove all images

Volumes

List Volumes:

$ docker volume ls
$ docker volume ls -f dangling=true # show dangling

** Remove Volume **

$ docker volume rm myvolume
$ docker volume rm myvolume -f # force remove

Prune Volumes:

$ docker volume prune 

WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
07c7bdf3e34ab76d921894c2b834f073721fccfbbcba792aa7648e3a7a664c2e
my-named-vol

Total reclaimed space: 36 B

System Prune:

This command helps clean up the whole system:

$ docker system prune

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

And for even deeper cleaning:

docker system prune -a --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

I wanted give you an organized list of how to systematically think about your docker system which will make it easier understand how elements work together. If there are any other suggestions you have to keep your deck spotless, please share in the comments!