Docker - Clean (Removing Image and Container)

> Virtualization - (Virtual Machine|Container) > Docker

1 - About

Docker host disk sizing management.

Docker stores all layers/images in its file formate (i.e. aufs) in default /var/lib/docker directory.

2 - Management

2.1 - Show space

docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              16                  3                   3.774GB             2.227GB (59%)
Containers          5                   0                   233.1MB             233.1MB (100%)
Local Volumes       2                   1                   35.73MB             0B (0%)
Advertising

2.2 - Containers

Remove the container that where not used months ago

docker ps --filter "status=exited" | grep 'months ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm

See also: docker container prune

2.3 - Images

2.3.1 - Untagged

Ie with the tag none.

docker rmi $(docker images -q -f dangling=true)
# or
docker image ls | grep 'none' | awk '{print $3}' | xargs --no-run-if-empty docker image rm

2.3.2 - Old

docker image ls | grep 'months ago' | awk '{print $1"\:"$2}' | xargs --no-run-if-empty docker image rm

2.4 - Volume

2.4.1 - Orphaned / Unused

To delete orphaned volumes in Docker 1.9 and up

docker volume prune 
Advertising

2.4.2 - Dangling volume

docker volume rm $(docker volume ls -qf dangling=true)

2.5 - System

docker system prune

clean up containers, images, volumes, and networks all in one command.

3 - Documentation / Reference