vsupalov

Cleaning Up After Docker

A few commands you can run, when Docker stuff is taking up too much space.

[ docker ]

Have you been running and re-running docker-compose lately? If you’re using Docker for development purposes, you’re running lots of containers, building new images and probably have a chunk of space on your disk which is occupied by stuff you don’t need.

Just chucking along, and not doing anything about it, will soon see your root directory fill up 99% of your available space. Weird things tend to happen when you max out your capacity - here’s what you need to do to clean up after Docker.

Use these with caution! If you have any valuable data dangling around in Docker containers:

  • Mount local directories for important data.
  • Back up your volumes.
  • Shame on you.

The stuff which is taking up most space, are old images (previous builds), stopped containers and their volumes.

Docker Compose

If you just want to clean up the data of a particular docker-compose stack, run

$ docker-compose down -v --rmi all --remove-orphans

This will take care of taking down the volumes, images and dangling stuff. Anyway, you might wannt to look into the following as well.

Getting a List of Offenders

docker ps -a -q is your friend. It lists all container identifiers, and only them in a nice list. The same goes for docker images -q - all image identifiers just for you. Regarding volumes, docker volume ls -q is your friend.

Fall most commands, you can pass on a list of ids to perform the operation on. This happens with the bash notation $(command), which executes a command and insers its output into the place it occupies.

Cleaning Up

The following commands will take care of your mess. Once again, make sure you’re not removing stuff you might miss later on. Take heed. The last command for example, will remove volumes related to docker-compose stacks if the containers are down. That’s usually NOT what you want to do.

The -f arguments help to reduce the number of warnings you get (upon trying to remove a running container for example) and make the intentions clearer. If you want to just remove all images for example, just leave out the -f argument when getting a list of images.

$ docker rm -v $(docker ps -aq -f 'status=exited')
$ docker rmi $(docker images -aq -f 'dangling=true')

# ATTENTION: this will also remove volumes of docker-compose if the containers are barely stopped
$ docker volume rm $(docker volume ls -q -f 'dangling=true')

Since Version 1.25

A new change came along, which hugely simplifies the above operations, and reduces them to one single command.

$ docker system prune

This will remove all unused containers, volumes, networks and images (both dangling and unreferenced).

If you’d like more control than the above, you can limit the pruning down to a single part, like the images, by issuing something like:

$ docker image prune

Happy Dockering!

Master Docker ARG and ENV in 5 Days

Sign up to the free 5-day email course, and learn all you need to know about using environment and build-time variables with Docker. Get actionable advice, best practices and nifty tricks to use for your next project.