Tuesday, April 01, 2014

Docker: Bulk Remove Images and Containers

I’ve just started looking at Docker. It’s a cool new technology that has the potential to make the management and deployment of distributed applications a great deal easier. I’d very much recommend checking it out. I’m especially interested in using it to deploy Mono applications because it promises to remove the hassle of deploying and maintaining the mono runtime on a multitude of Linux servers.

I’ve been playing around creating new images and containers and debugging my Dockerfile, and I’ve wound up with lots of temporary containers and images. It’s really tedious repeatedly running ‘docker rm’ and ‘docker rmi’, so I’ve knocked up a couple of bash commands to bulk delete images and containers.

Delete all containers:

sudo docker ps -a -q | xargs -n 1 -I {} sudo docker rm {}

Delete all un-tagged (or intermediate) images:

sudo docker rmi $( sudo docker images | grep '<none>' | tr -s ' ' | cut -d ' ' -f 3)

5 comments:

Anonymous said...

There are several handy shortcuts documented on the issue requesting an integrated command to take care of this: https://github.com/dotcloud/docker/issues/928

docker rm `docker ps -a -q`
docker rmi `docker images -q`

Aaron Robinson said...

Take care with those suggested commands. The images one removes all your local images.

Unknown said...

I created a file named imglike and put this in it:

-----------------
#!/bin/bash
PATTERN=$(echo "$1" | sed -e 's:\.:\\.:g' -e 's:*:.*:g')
docker images | grep -P "^(?!|REPOSITORY)$PATTERN" | awk -v OFS=: '{print $1,$2}'
-----------------

Then
chmod +x imglike
ln -sf /path/to/imglike /usr/local/bin/

Then I can do
docker rmi -f $(imglike "repo/*")

In other words I can use * wildcards for docker images.

Regards,
Bobi

Clark said...

Borislav, awesome - thanks for this!

Anonymous said...

Simplest command to delete containers based on status=exited

`docker rm -v $(docker ps -aq -f status=exited)`