As I create/debug a docker image/container docker seems to be leaving all sorts of artifacts on my system. (at one point there was a 48 image limit) But the last time I looked t
Update: There are built-in filters in the docker cli that let one properly display containers that meet certain criteria. These bash functions are taken from one of the core maintainers' dotfiles:
dcleanup(){
docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
}
del_stopped(){
local name=$1
local state=$(docker inspect --format "{{.State.Running}}" $name 2>/dev/null)
if [[ "$state" == "false" ]]; then
docker rm $name
fi
}
Original Answer
A helper script I've created for my own use:
#!/bin/bash
# options:
# remove stopped containers and untagged images
# $ dkcleanup
# remove all stopped|running containers and untagged images
# $ dkcleanup --reset
# remove containers|images|tags matching {repository|image|repository\image|tag|image:tag}
# pattern and untagged images
# $ dkcleanup --purge {image}
# everything
# $ dkcleanup --nuclear
if [ "$1" == "--reset" ]; then
# Remove all containers regardless of state
docker rm -f $(docker ps -a -q) 2>/dev/null || echo "No more containers to remove."
elif [ "$1" == "--purge" ]; then
# Attempt to remove running containers that are using the images we're trying to purge first.
(docker rm -f $(docker ps -a | grep "$2/\|/$2 \| $2 \|:$2\|$2-\|$2:\|$2_" | awk '{print $1}') 2>/dev/null || echo "No containers using the \"$2\" image, continuing purge.") &&\
# Remove all images matching arg given after "--purge"
docker rmi $(docker images | grep "$2/\|/$2 \| $2 \|$2 \|$2-\|$2_" | awk '{print $3}') 2>/dev/null || echo "No images matching \"$2\" to purge."
else
# This alternate only removes "stopped" containers
docker rm -f $(docker ps -a | grep "Exited" | awk '{print $1}') 2>/dev/null || echo "No stopped containers to remove."
fi
if [ "$1" == "--nuclear" ]; then
docker rm -f $(docker ps -a -q) 2>/dev/null || echo "No more containers to remove."
docker rmi $(docker images -q) 2>/dev/null || echo "No more images to remove."
else
# Always remove untagged images
docker rmi $(docker images | grep "" | awk '{print $3}') 2>/dev/null || echo "No untagged images to delete."
fi
exit 0
source
To your questions:
how does one properly cleanup?
no official way yet, just helper scripts and functions like the above.
as I was manually deleting images more started to arrive. huh?
you might have been deleting images that were built on top of others that became "untagged" when you tried to delete them.
how much disk space should I really allocate to the host?
depends on the types of images you plan to use. Know that running a 500 mb image multiple times doesn't use (500mb X number of containers) space. The containers reuse the same image and just add whatever they change when running on top. So think from an image storing perspective, not a container runtime one regarding storage.
will running daemons really restart after the next reboot?
By default, they are stopped when the host reboots. You need to run with docker run --restart=True
to automatically start up again when the host reboots.