Docker cleanup commands

Running Docker containers also includes a little housekeeping to keep your Docker hosts running optimal and not wasting resources. This blog post provides an overview of which commands you can use.

Currently there are a lot of blog posts and stackoverflow questions that talk about clean up commands for old Docker versions, that are not very useful anymore. In this blog post I will try to keep them updated with newer versions of Docker.

Current Docker version as of 2016/07/20: 1.11 (stable), 1.12 (beta)

Clean up old containers

Originally copied from this blog post: source

These commands can be dangerous! So don’t just copy/paste them without at least having a clue what they do.

# Kill all running containers:
docker kill $(docker ps -q)

# Delete all stopped containers (including data-only containers):
docker rm $(docker ps -a -q)

# Delete all exited containers
docker rm $(docker ps -q -f status=exited)

# Delete ALL images:
docker rmi $(docker images -q)

# Delete all 'untagged/dangling' (<none>) images ():
docker rmi $(docker images -q -f dangling=true)

Clean up old volumes

When a container defines a VOLUME it will not automatically delete this volume when the container gets deleted. Some manual clean up is needed to get rid of these “dangling” volumes.

Originally found on Stackoverflow: source

# List all orphaned volumes:
docker volume ls -qf dangling=true

# Eliminate all of them with:
docker volume rm $(docker volume ls -qf dangling=true)
comments powered by Disqus