Over a million developers have joined DZone.

De-Cluttering Your Docker Environment

Is Docker messy? Do your images have images within images wrapped in images? Fear not! Here are some commands that will keep things nice and tidy (and help with testing).

· Cloud Zone

Download the Essential Cloud Buyer’s Guide to learn important factors to consider before selecting a provider as well as buying criteria to help you make the best decision for your infrastructure needs, brought to you in partnership with Internap.

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 below!

The Cloud Zone is brought to you in partnership with Internap. Read Bare-Metal Cloud 101 to learn about bare-metal cloud and how it has emerged as a way to complement virtualized services.

docker ,dev environment

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}