Docker Command Remove / Clean Up
When working with Docker, it’s essential to clean up unused containers, images, and other resources to free up disk space and keep your system tidy. Here are three options for cleaning up Docker resources:
Option 1: Using docker-clean
You can use a third-party tool called docker-clean to help you clean up Docker resources more efficiently. This tool provides a simple command to remove stopped containers, dangling volumes, and unused images.
To use docker-clean
, follow these steps:
- Install
docker-clean
if you haven’t already:
|
|
- Run the
docker-clean
container to clean up Docker resources:
|
|
This command will remove stopped containers, volumes without containers, and images with no containers.
Option 2: Manual Cleanup
If you prefer not to use third-party tools, you can manually clean up Docker resources using a series of Docker commands:
Remove Containers with Exited Status
To remove containers with an exited status, you can use the following commands:
|
|
These commands will remove containers that are in either the “exited” or “created” status.
Remove Unused Images
To remove unused (dangling) images, you can use the following command:
|
|
This command will delete images that are not associated with any containers.
Option 3: Using docker system prune
(Advanced)
The docker system prune
command is a built-in Docker command for cleaning up various types of unused data, including containers stopped, volumes without containers, and images with no containers.
To use docker system prune
, simply run the following command:
|
|
This command will interactively prompt you to confirm the cleanup before proceeding.
Note: Be cautious when using the docker system prune
command, as it will remove more data than just stopped containers and unused images. It will also remove other unused data like networks and build cache.
Choose the option that best suits your needs for cleaning up Docker resources based on your preference and requirements.