DEV Community

Cover image for How to clean up your Docker environment
Deepu K Sasidharan
Deepu K Sasidharan

Posted on • Edited on • Originally published at deepu.tech

How to clean up your Docker environment

Originally published at deepu.tech.

Cleanup your Docker setup

If you are using Docker on your PC or Mac, over time it is gonna accumulate a lot of Junk, the majority of which being dangling images and orphan volumes. It could take up a lot of space in your machine.

You should clean this up once in a while, thankfully there are some docker commands to help us here and along with some bash magic, we can nicely do this in 3 easy steps.

Clean up old containers

Docker has a docker rm command to remove containers, we can use this along with some docker ps filters to remove all containers that are not being used currently. This is perfectly fine as it doesn't affect anything that is running and if you want to use any of the removed images again, docker will download it for you.

So the below command should do the trick.

docker rm -v $(docker ps -a -q -f status=exited);
Enter fullscreen mode Exit fullscreen mode

docker ps -a -q -f status=exited provides a list of container Ids that are in exited status and docker rm -v removes those along with their associated volumes. Run docker rm --help and docker ps --help to see what the flags mean.

Note: If you want anything from these volumes, you should back it up before doing this.

Clean up dangling volumes

A dangling volume is one that exists and is no longer connected to any containers. There is a similar rm command for volumes as well. We can use this along with docker volume ls command with a filter to remove volumes that are dangling. Below is the command for that.

docker volume rm $(docker volume ls -q -f dangling=true);
Enter fullscreen mode Exit fullscreen mode

docker volume ls -q -f dangling=true returns the volume names that are not connected to any containers and docker volume rm removes them. Run docker volume rm --help and docker volume ls --help to see what the flags mean.

Note: If you want anything from these volumes, you should back it up before doing this.

Clean up dangling images

Finally, we need to clean up dangling images. Docker images are made up of multiple layers and dangling images are layers that have no relationship to any tagged images. They no longer serve any purpose and consume disk space and hence can be safely removed. We can use the docker image prune -a command from Docker to remove unused images but for some reason, this command was not working for me and hence I had to resort to the image rm command as below.

docker image rm $(docker images -q -f dangling=true);
Enter fullscreen mode Exit fullscreen mode

docker images -q -f dangling=true returns the image names that are not related to any tagged images and docker image rm removes them. Run docker image rm --help and docker images --help to see what the flags mean.

Clean up everything

Update: Seems like docker provides a native command to clean up everything.

docker system prune --volumes
Enter fullscreen mode Exit fullscreen mode

You can run this to achieve the same result as below command and a bit more. Thanks to cyberjack for pointing it out. I'll leave the below for educational purposes.

Now we can add these commands as a handy bash function so that we can do this cleanup with a single command like I always do. It frees up a lot of disk space and helps when I'm having some weird cache issues with docker-compose 😉.

Add the below function to your ~/.bashrc or ~/.zshrc and reload your terminal or source the file by running . ~/.zshrc or . ~/.bashrc.

function dpurgeall(){
  docker rm -v $(docker ps -a -q -f status=exited);
  docker volume rm $(docker volume ls -qf dangling=true);
  docker image rm $(docker images -qf dangling=true);
}
Enter fullscreen mode Exit fullscreen mode

Now you can simply run dpurgeall and it will clean up your Docker environment. If there is nothing to clean up the command will just exist so nothing to worry there.


If you like this article, please leave a like or a comment.

You can follow me on Twitter and LinkedIn.

Cover image credit: Photo by chuttersnap on Unsplash

Top comments (10)

Collapse
 
cyberjack profile image
CyberJack

Nice function, but did you know Docker has it's own clean up mechanism? It's called "docker system prune" and it's available since api version 1.25. It can be used to remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

By default it will remove all stopped containers, all networks not used by at least one container, all dangling images and all build cache.

When adding the -a and --volumes parameters it will remove all stopped containers, all networks not used by at least one container, all volumes not used by at least one container, all images without at least one container associated to them and all build cache.

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Wow cool. I didn't know that and when I was originally writing this function few months ago it never came up in any of the resources I found. Cool. I'll try it out.

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Well talk about making the entire post irrelevant 😉

Thread Thread
 
cyberjack profile image
CyberJack • Edited

I don't think its irrelevant at all. The system prune command only works when you want to clean your entire docker setup. All these commands give you more control when you want to clean only certain parts of your docker setup.

Collapse
 
m1well profile image
Michael Wellner • Edited

my actual command:

# clean up docker images and volumes
dockercleanup() {
  docker system df
  docker system prune -f
  docker volume prune -f
  docker image prune -f
  docker system df
}

from github.com/m1well/env-setup

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Seems like you can combine 2 and 3 with --volume flag, 4 never worked for me, don't know why

Collapse
 
jannikwempe profile image
Jannik Wempe

Thanks for that handy little scripts.

Can you tell the difference to docker system prune?

Collapse
 
deepu105 profile image
Deepu K Sasidharan

As I mentioned in another comment. I wasn't aware of it. I need try it and compare. Once I do I'll update the post

Collapse
 
sso profile image
Sall

Really respect people who produce posts likes this one. Simple, Focused and Effective. Thank you

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Thank you