Pruning Objects in Docker
Pruning in Docker refers to the process of cleaning up unused Docker objects (such as containers, images, volumes, and networks) to free up system resources. Docker provides commands to prune unused or dangling resources that are no longer in use.
Types of Objects You Can Prune in Docker:
-
Containers: Stopped containers that are not being used.
-
Images: Unused images, including dangling images.
-
Volumes: Unused volumes that are not connected to any containers.
-
Networks: Unused networks not connected to any containers.
Docker Prune Commands
1. Prune Stopped Containers
To remove all stopped containers (containers that are no longer running):
-
This will prompt for confirmation before removing all stopped containers. If you want to skip the confirmation prompt, you can use the
-f
(force) flag:
2. Prune Unused Docker Images
You can remove unused Docker images (images that are not tagged and are not used by any containers):
-
This will remove only dangling images (untagged images that are not used by any containers).
-
To remove all unused images (not just dangling), use the
-a
flag: -
Similar to other prune commands, you can use the
-f
flag to force the pruning without a prompt:
3. Prune Unused Volumes
Docker volumes are used for persistent storage, but over time, unused volumes may accumulate. To remove unused volumes:
-
This will remove all volumes that are not being used by any container. Use
-f
to skip the confirmation prompt:
4. Prune Unused Networks
If there are unused networks (networks that are not connected to any containers), you can remove them:
-
Use the
-f
flag to skip the confirmation prompt:
5. Prune All Unused Objects
You can remove all unused Docker objects (containers, images, volumes, and networks) with a single command:
-
This will remove:
-
Stopped containers
-
Unused networks
-
Dangling images
-
Unused volumes (with the
--volumes
flag)
-
-
To include unused volumes in the prune operation, use the
--volumes
flag: -
If you want to skip the confirmation prompt, use the
-f
flag: -
To also remove unused images (not just dangling ones), use the
-a
flag:
This will clean up almost everything that is not being used, giving you a fresh environment.
Summary of Docker Prune Commands
-
Prune Stopped Containers:
-
Prune Unused Images:
-
Prune All Unused Images:
-
Prune Unused Volumes:
-
Prune Unused Networks:
-
Prune All Unused Objects (Containers, Images, Volumes, Networks):
-
Prune All Unused Objects and Volumes:
-
Prune All Unused Objects, Including All Images and Volumes:
Best Practices
-
Prune Regularly: Prune unused Docker objects regularly to free up system resources.
-
Careful with
-a
flag: Be cautious when using the-a
flag with pruning commands (likedocker image prune -a
), as it will remove all unused images, even if they are not dangling. -
Avoid Removing Volumes: Volumes store persistent data. Be careful when pruning volumes with
docker volume prune
ordocker system prune --volumes
, as you might delete important data.