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:

1
docker ps -a

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

1
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):
1
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):
1
docker volume ls -qf dangling=true | xargs -r docker volume rm
  • To delete images not longer referenced by any tag ():
1
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:
1
docker images -qf dangling=true | xargs -r docker rmi
  • To delete temporary files (blobs which were left by some interrupted docker pull):
1
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!