I’m in love with Docker but man it can eat usable disk space like it’s nothing. Docker doesn’t care about OS environment or version, it will slowly devour every bit of available disk space and there’s nothing you can do about it. Or can we?
While we cannot prevent Docker from doing what it’s doing best, we can clean it. Before we can start, we first need to find available Docker version. This information is important as it will tell us which solution is available for us.
Open a CLI of your choice and execute this line:
docker -v
The output should look like this:
Docker version 17.04.0-ce, build 4845c56
If you are using a non-enterprise variation of Docker you should be aware that Docker versions are separated into two categories:
  • Docker engine releases also known as Docker CS (Commercially Supported) – Everything until version 1.13.1
  • Current Docker CE (Community Edition) releases – Everything above version 17.04.0-ce
      You should also be aware that there’s nothing between versions 1.13.1 and 17.04.0-ce.
      This tutorial will cover two sets of available solutions. The first approach works on all versions up to 1.12.6, the second approach works on 1.13.0 and above (including 17.04.0-ce and above).

      Docker <= 1.12.6

      First, we need to clean unused Docker containers. These containers are no longer active but they are still taking available space.
      To list these containers do this:
      docker ps -a
      To remove these containers do this:
      docker rm -v $(docker ps -a -q -f status=exited) 2>/dev/null
      Why using 2>/dev/null? If there aren’t any unused containers and you execute above line, Docker will throw this error: “docker rm” requires at least 1 argument(s). 2>/dev/null will direct error messages to null, so it will do nothing if all image is cleaned up.
      Second, we need to clean dangling images. Again, docker will not delete them on its own.
      docker rmi $(docker images -f "dangling=true" -q) 2>/dev/null
      Third, remove unused volumes if there are any. This should not happen often as docker rm -v should take care of them, more specific the -v parameter.
      To list unused volumes do this:
      docker volume ls -qf dangling=true
      To remove unused volumes do this:
      docker volume rm $(docker volume ls -qf dangling=true) 2>/dev/null

      Docker > 1.13.0

      If you’re lucky enough to have above version or higher, do these:
      docker image prune
      docker container prune
      docker volume prune
      Or if you want to do clean everything (used and unused) do this:
      docker system prune -a

      Who Am I?

      Between working as a senior Java developer in one of the largest insurance companies in the world and traveling, in my free time, I work as a professional mobile development adviser. I'm also a major jQuery Mobile supporter back at StackOverflow and a forum moderator at the official Ionic Framework forum.