A collection of docker command snippets for common tasks
created by on 2014-09-08


Remove Stopped Containers

sudo docker rm $(sudo docker ps -a -q)

This will try to remove all containers that are stopped and running. The ones which are are running cannot be removed.
If you want it a little bit more precise you can use this command instead to remove only the stopped containers:

docker ps -a | grep "days ago"| awk '{print $1}' | xargs docker rm

Remove all untagged images

sudo docker images --no-trunc | grep none | awk '{print $3}' | xargs sudo docker rmi

This command is particularly useful if you want to free some disk space because images can use a considerable amount of space.

Remove all images from a given namespace

sudo docker images <namespace>/* | awk '{print $3}' | xargs docker rmi

Kill all running containers with a certain name

docker ps -q | xargs docker inspect --format '{{.Name}}' | grep "$containerNameFragment" | sed 's:/::' | xargs docker kill

Saving Space for Images

Always reference the a specific tagged version of an image, otherwise docker will download all tags of an image (e.g. use “redis:latest” instead of “redis”).

Service Discovery


HA Proxy

Running docker as a non-privileged user

sudo groupadd docker
sudo gpasswd -a ${USER} docker
sudo service docker restart

Then log-out and back in.


Lessons Learned



“unable to mount sys as readonly” when running image

Edit the file /etc/sysconfig/docker:

other_args="--exec-driver=lxc --selinux-enabled"
Fork allmark on GitHub