Clearing Docker images and containers.

13th November 2016

Why is this necessary?

There’s no doubt that Docker is a great tool, but it does tend to leave a lot of stuff lying around that soon adds up to a fair chunk of disk space.

The Docker team have good reasons for this, but sometimes you just want to freshen up, to start again or simply to clear down redundant stuff. Much of it, though, is hidden - in the sense of ‘out of sight, out of mind’ rather than deliberately obscured.

There are ways, though, to tidy up (the following two will not work on Windows).

A gentle clear-out.

If you want a gentle way of doing things, you could try (on *nix):

docker images   ## to see what there currently is
docker images -q --filter "dangling=true" | xargs docker rmi -f
docker ps -a
docker rm -v $(docker ps -a -q -f status=exited)

A proper spring clean.

This (on *nix) doesn’t just check for dangling images:

docker images   ## to see what there currently is
docker images -q | xargs docker rmi -f
docker ps -a
docker rm -v $(docker ps -a -q -f status=exited)

Other options.

There is a Docker command built-in which is sometimes simpler:

docker system prune