Create your FREE Codefresh account and start making pipelines fast. Create Account

Useful tricks to clean up your Docker dev environment

2 min read

Many of us who develop with Docker often find that we are:

  • Building layers upon layers of  images
  • Running a whole fleet of containers originating from various base images and different versions of our own apps

After all, being able to quickly build, spin up and test various code and environment combinations is one of the greatest benefits of container-based development. And there’s no better way to test your changes than running a few environments in parallel and switching between them. It’s blue-green deployment and A/B testing at your fingertips! But even the strongest development machine has resource limitations. Eventually, we need to remove images and kill some containers just to be able to build and run new versions.

So here are a few useful command-line tricks to help you clean up your working environment.

    • Remove all stopped  containers:
docker rm $(docker ps -a -q)
    • Remove all untagged  images:
docker images -q --filter "dangling=true" | xargs docker rmi

Note: Use ‘docker rmi -f‘ to force the removal of images even if there are some stopped containers based on them .

    • Remove all containers based on specific  image:
docker ps --filter ancestor=codefresh/golang:1.1 -q | xargs -l docker stop

Note : You will need to replace codefresh/golang:1.1 with your own <repository>:<tag> in the last command.
If you omit the :<tag> –  ‘docker ps’ will return only the containers based on the ‘latest’ tag of the specified image. They are the ones that will get stopped.

    • Remove unused data volumes ( thanks to Pierre F. for the suggestion )
docker volume rm $(docker volume ls -qf dangling=true)

Got more tricks of your own? Please leave them in comments.

Codefresh is a Docker automation & collaboration platform for agile teams. Get your free account.

Anton Weiss

Ant(on) Weiss is a DevOps Evangelist and Enabler. He's been architecting software delivery processes at large and small companies for the last 15 years. Today he's the CEO and Principal Consultant at Otomato and deeply involved with technical training and public speaking.

8 responses to “Useful tricks to clean up your Docker dev environment

  1. Hello, thanks for this article !

    You also have this one to clean the unused volumes:

    docker volume rm $(docker volume ls -qf dangling=true)

  2. Anton Weiss says:

    Hi Pierre!
    Indeed – cleaning up the data volumes is also very important.
    Thanks for the feedback! We’ll add it to the post body.


  3. On MacOS Sierra Beta, I get an error on this one, removing the -l on the ‘xargs’ and it seems to work fine.

    ➜ ~ docker images -q –filter “dangling=true” | xargs -l docker rmi
    xargs: illegal option — l
    usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
    [-L number] [-n number [-x]] [-P maxprocs] [-s size]
    [utility [argument …]]

    1. Hi Jim!
      You’re right – the ‘-l’ option is unsupported in OSX version of xargs.
      As a matter of fact – in this case it can be omitted on Linux as well.
      Thanks, we’ll update the post.

    1. Hmm, this looks like a great little tool! Wasn’t aware of it. Thanks a lot for bringing it to our attention, Zachary!

Leave a Reply

* All fields are required. Your email address will not be published.