Using Docker has become very handy to must of us when working with multiple environments. However, one problem that we noticed is the incredible disk usage of it. So in this post I will list useful commands that could help us solving the issue.

First, you must check and remove stopped Docker containers that will no longer be used.

To list this you can run:

   docker ps -a

After selecting the unused containers, you can remove them with the following command:

   docker rm -v {name or hexadecimal container identifier}

The flag -v is used to also delete any associated volume.

Then, following command should be executed

  • To delete “dead” containers. (containers with status=dead whose creation failed):
   docker ps -f status=dead --format '{{ .ID }}' | xargs -r docker rm -v
  • To delete volumes not associated with any containers ( This usually happens when container are deleted without -v flag of docker rm command):
docker volume ls -qf dangling=true | xargs -r docker volume rm
  • To delete images not longer referenced by any tag (<none>):
docker images --digests --format '{{.Repository}}:{{.Tag}}@{{.Digest}}' | sed -ne 's/:<none>@/@/p' | xargs -r docker rmi
  • To delete images not longer referenced by any name:
docker images -qf dangling=true | xargs -r docker rmi
  • To delete temporary files (blobs which were left by some interrupted docker pull):
rm -f /var/lib/docker/tmp/*

Note: This files are useful in case we want to resume the docker pull command.

Hope you find this post useful and keep Dockering!