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:

  1. Install docker-clean if you haven’t already:
1
docker pull zzrot/docker-clean
  1. Run the docker-clean container to clean up Docker resources:
1
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock zzrot/docker-clean

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:

1
2
docker rm $(docker ps -q -f status=exited)
docker rm $(docker ps -q -f status=created)

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:

1
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

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:

1
docker system prune

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.

0%