DEV Community

Cover image for How to clean up your Docker environment

How to clean up your Docker environment

Deepu K Sasidharan on December 06, 2019

Originally published at deepu.tech. Cleanup your Docker setup If you are using Docker on your PC or Mac, over time it is gonna accumula...
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