0
0
DockerHow-ToBeginner · 3 min read

How to Clean Up Docker Resources: Containers, Images, Volumes, Networks

Use docker system prune to remove unused containers, networks, images, and optionally volumes. For more control, use commands like docker container prune, docker image prune, and docker volume prune to clean specific resource types.
📐

Syntax

The main commands to clean Docker resources are:

  • docker system prune [options]: Removes all unused containers, networks, images, and optionally volumes.
  • docker container prune: Removes all stopped containers.
  • docker image prune [options]: Removes unused images.
  • docker volume prune: Removes unused volumes.
  • docker network prune: Removes unused networks.

Options like -a remove all unused images, not just dangling ones, and --volumes includes volumes in docker system prune.

bash
docker system prune [--volumes] [-a]
docker container prune
docker image prune [-a]
docker volume prune
docker network prune
💻

Example

This example shows how to remove all stopped containers, unused images, unused networks, and unused volumes to free up space.

bash
docker system prune --volumes -a
Output
WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all images without at least one container associated to them - all build cache - all unused volumes Are you sure you want to continue? [y/N] y Deleted Containers: <container_id_1> Deleted Images: <image_id_1> Deleted Volumes: <volume_id_1> Deleted Networks: <network_id_1> Total reclaimed space: 1.2GB
⚠️

Common Pitfalls

Common mistakes when cleaning Docker resources include:

  • Running docker system prune without --volumes and expecting volumes to be removed (they are not removed by default).
  • Using docker image prune without -a only removes dangling images, not all unused images.
  • Accidentally deleting important volumes or images by running prune commands without checking what will be removed.
  • Not confirming the prompt and expecting automatic cleanup.
bash
Wrong way:
docker system prune
# Volumes are NOT removed by default

Right way:
docker system prune --volumes -a
# Removes all unused containers, images, networks, and volumes
📊

Quick Reference

CommandDescription
docker system pruneRemove all stopped containers, unused networks, dangling images, and build cache
docker system prune --volumesAlso remove unused volumes
docker system prune -aRemove all unused images, not just dangling ones
docker container pruneRemove all stopped containers
docker image pruneRemove dangling images
docker image prune -aRemove all unused images
docker volume pruneRemove unused volumes
docker network pruneRemove unused networks

Key Takeaways

Use docker system prune --volumes -a to clean all unused Docker resources including volumes.
Without --volumes, volumes are not removed by docker system prune.
Use specific prune commands for targeted cleanup like docker container prune or docker volume prune.
Always review the prompt before confirming to avoid deleting important data.
Pruning helps free disk space and keeps your Docker environment tidy.