docker image prune
to remove unused images:$ docker image prune --help
Usage: docker image prune [OPTIONS] Remove unused images Options: -a, --all Remove all unused images, not just dangling ones --filter filter Provide filter values (e.g. 'until=<timestamp>') -f, --force Do not prompt for confirmationFirst, let's list existing images:
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE zookeeper-multi-stage 3.6.1 673b3d044115 9 minutes ago 40.8MB <none> <none> 95c0f0969c15 9 minutes ago 55.1MB alpine latest a24bb4013296 2 months ago 5.57MBBy default, only regular and dangling images are shown.
$ docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE zookeeper-multi-stage 3.6.1 673b3d044115 12 minutes ago 40.8MB <none> <none> 95c0f0969c15 12 minutes ago 55.1MB <none> <none> 95e1e698e91f 12 minutes ago 19.8MB <none> <none> 54616be057e7 12 minutes ago 7.4MB alpine latest a24bb4013296 2 months ago 5.57MBRemove dangling images:
$ docker image prune
WARNING! This will remove all dangling images. Are you sure you want to continue? [y/N] y Deleted Images: deleted: sha256:95c0f0969c156a7bba1c94636a04e903ff01bba68b2b6938d69e9dbd3e0df71e deleted: sha256:2ffccaa21550779cbd06bb923d287fcb9956944d9580553d7b0351f2449f62d1 deleted: sha256:95e1e698e91fc643b11bd4f1b725966348e7125aaac4e6fc2a00dd615b2f23e3 deleted: sha256:c6d4e880f3e27836c4b13e4125933938d185eebad9580a0bf5e0dc368cf225f0 deleted: sha256:54616be057e7549cd225a67e06b515eeafbe85d6e9b69541cf60b43d61c1ddd1 deleted: sha256:8caf8b6e850679bfe3d3d4c1369d399e0a17c8c085f312eabf1454d9107a111a Total reclaimed space: 49.51MCheck that dangling images were deleted:
$ docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE zookeeper-multi-stage 3.6.1 673b3d044115 12 minutes ago 40.8MB alpine latest a24bb4013296 2 months ago 5.57MBIt's also possible to use this command to remove dangling images:
$ docker image rm $(docker image ls -q -f "dangling=true")
docker buildx prune
to remove build cache:$ docker buildx prune --help
Remove build cache Usage: docker buildx prune Remove build cache Options: -a, --all Include internal/frontend images --builder string Override the configured builder instance -D, --debug Enable debug logging --filter filter Provide filter values (e.g., "until=24h") -f, --force Do not prompt for confirmation --max-used-space bytes Maximum amount of disk space allowed to keep for cache --min-free-space bytes Target amount of free disk space after pruning --reserved-space bytes Amount of disk space always allowed to keep for cache --verbose Provide a more verbose output Experimental commands and flags are hidden. Set BUILDX_EXPERIMENTAL=1 to show them.Let's create a simple Dockerfile:
$ vi Dockerfile
FROM ubuntu:latestLet's build the Dockerfile:
$ docker build -t ubuntu-test:latest .Clear build cache:
$ docker buildx prune --all --verbose
WARNING! This will remove all build cache. Are you sure you want to continue? [y/N] y ID: 52cwy2cp9vd6od07xk2ux0xgk Created at: 2024-06-15 15:14:54.395180592 +0000 UTC Mutable: false Reclaimable: true Shared: false Size: 29.72MB Usage count: 1 Last used: 2 minutes ago Total: 29.72MB
docker container prune
to remove all stopped containers:$ docker container prune --help
Usage: docker container prune [OPTIONS] Remove all stopped containers Options: --filter filter Provide filter values (e.g. 'until=<timestamp>') -f, --force Do not prompt for confirmationLet's start a container:
$ docker container run ubuntu:latest
5447b485b834c8125a4b6fceb0aaf1be1f39fa36b8247320246ba9b792e52f39Check the container:
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES 5447b485b834 ubuntu:latest "/bin/bash" 6 seconds ago Exited (0) 5 seconds ago distracted_hooverNote that the container was started and executed the command /bin/bash, but then exited because no process was running in the foreground or background.
$ docker container prune
WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y Deleted Containers: 5447b485b834c8125a4b6fceb0aaf1be1f39fa36b8247320246ba9b792e52f39 Total reclaimed space: 0BIt's also possible to use this command to remove all containers with the status exited:
$ docker container rm $(docker container ls -a -q --filter "status=exited")
docker network prune
to remove all unused networks (not attached to a running container):$ docker network prune --help
Usage: docker network prune [OPTIONS] Remove all unused networks Options: --filter filter Provide filter values (e.g. 'until=<timestamp>') -f, --force Do not prompt for confirmationCreate a test network:
$ docker network create network1
1f1873ef27f8a4bedceef2dd3f9e687216d2f648b544ced4610bfcd55fb507baRemove unused networks:
$ docker network prune
WARNING! This will remove all custom networks not used by at least one container. Are you sure you want to continue? [y/N] y Deleted Networks: network1
docker volume prune
to remove all unused local volumes (not attached to a running container):$ docker volume prune --help
Usage: docker volume prune [OPTIONS] Remove unused local volumes Options: -a, --all Remove all unused volumes, not just anonymous ones --filter filter Provide filter values (e.g. "label=<label>") -f, --force Do not prompt for confirmationCreate a test volume:
$ docker volume create volume1
volume1Let's use the created volume:
$ docker container run --rm --volume volume1:/cvdata ubuntu:latest /bin/bash -c "echo 'hello docker volumes' > /cvdata/file1.txt"
d58173f7f8c81b429d0a0e015f864a4797c9ffb7f1c22bec2e3a1e0932f776dcLet's check the data in the volume:
$ docker container run --rm --volume volume1:/cvdata ubuntu:latest /bin/bash -c "cat /cvdata/file1.txt"
hello docker volumesLet's remove the unused volumes:
$ docker volume prune --all
WARNING! This will remove all local volumes not used by at least one container. Are you sure you want to continue? [y/N] y Deleted Volumes: volume1 Total reclaimed space: 21BYou could also remove specific volumes using the command
docker volume rm
:$ docker volume rm --help
Usage: docker volume rm [OPTIONS] VOLUME [VOLUME...] Remove one or more volumes. You cannot remove a volume that is in use by a container. Aliases: docker volume rm, docker volume remove Options: -f, --force Force the removal of one or more volumes
docker system prune
to remove all unused objects.
This will remove unused images, build cahce, stopped containers, and unused networks: $ docker system prune --help
Usage: docker system prune [OPTIONS] Remove unused data Options: -a, --all Remove all unused images not just dangling ones --filter filter Provide filter values (e.g. "label=<key>=<value>") -f, --force Do not prompt for confirmation --volumes Prune anonymous volumesRemove unused objects (note the option --volumes to also remove unused local volumes):
$ docker system prune --volumes
WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all volumes not used by at least one container - all dangling images - all dangling build cache Are you sure you want to continue? [y/N] y Deleted Containers: 437d593134a15ce50fd9427713cdeabfa40eb94bf2577684a2317bf213afb35c Deleted Networks: network1 Deleted Volumes: volume1 Deleted Images: deleted: sha256:95c0f0969c156a7bba1c94636a04e903ff01bba68b2b6938d69e9dbd3e0df71e deleted: sha256:2ffccaa21550779cbd06bb923d287fcb9956944d9580553d7b0351f2449f62d1 deleted: sha256:95e1e698e91fc643b11bd4f1b725966348e7125aaac4e6fc2a00dd615b2f23e3 deleted: sha256:c6d4e880f3e27836c4b13e4125933938d185eebad9580a0bf5e0dc368cf225f0 deleted: sha256:54616be057e7549cd225a67e06b515eeafbe85d6e9b69541cf60b43d61c1ddd1 deleted: sha256:8caf8b6e850679bfe3d3d4c1369d399e0a17c8c085f312eabf1454d9107a111a Total reclaimed space: 49.51M