Posted by & filed under Docker.

Managing images and containers in Docker can get messy. After a dozen or so `run` commands without specifying `–rm`, temporary containers can quickly build up and cause maintenance headaches. Broken builds can also accrue lots of images that are no longer used and just taking up space. While Docker’s management commands work very well, there are a few nagging use-cases that aren’t supported.

RemoveĀ all containers

docker ps -a | tr -s ' ' | cut -d ' ' -f 1 | tail -n+2 | xargs docker rm

Not to be used lightly, this will remove *all* containers that are running or stopped.

Remove running containers

docker ps | tr -s ' ' | cut -d ' ' -f 1 | tail -n+2 | xargs docker rm

Remove running containers with names starting with “ubuntu”

docker ps | tr -s ' ' | grep '^ubuntu' | cut -d ' ' -f 1 | tail -n+2 | xargs docker rm

Remove all images

docker images | tr -s ' ' | cut -d ' ' -f 3 | tail -n+2 | xargs docker rmi

Again, not to be used lightly, this will remove *all* images from Docker.

Remove all images starting with names starting with “ubuntu”

docker images | tr -s ' ' | grep '^ubuntu' | cut -d ' ' -f 3 | tail -n+2 | xargs docker rmi

 

— the behavior-driven marketing platform for developers.

 

 

One Response to “Docker Image and Container Maintenance Cheat Sheet”

  1. Alexander

    You can also shorten.
    Remove all containers

    docker rm `docker ps -aq`

    or

    docker ps -aq | xargs docker rm

    Remove all images

    docker images -q | xargs docker rmi

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *