Cleaning up after Docker

Often times you want to remove all docker images and all docker containers. When you're experimenting with images, it gets a bit painful to manually remove each image / container individually.

To remove all containers:

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

To remove all images:

docker rmi $(docker images -q)

Now when you run

docker ps -a

All containers should be gone

Running

docker images

you might still see one or two images, these will probably require the -f flag to force remove them.

Comments (4)

Anthony Lapenna's photo

Nice, note that you can use the following one-liners too:

# Remove all dangling images (unused images)
$ docker rmi $(sudo docker images -q -f "dangling=true")

# Remove all dangling volumes (unused volumes)
$ docker volume rm $(docker volume ls -q -f "dangling=true")
Gaël Reyrol's photo

You should use docker-gc by Spotify, it's a garbage collector for docker images and containers.

github.com/spotify/docker-gc

Jaydeep Solanki's photo

Since docker v1.13, a few new commands were added

# shows used space, similar to the unix tool df
$ docker system df

# Removes all unused containers, volumes, networks and images (both dangling and unreferenced)
$ docker system prune

More about those commands: docs.docker.com/engine/reference/commandlin..

Jan Vladimir Mostert's photo

Idea Incubator, Full Stack Platform Architect

This looks useful!