Docker Remove Unused Containers

In this quick guide, we will discuss how to identify and remove unused Docker containers, ensuring a leaner and more efficient Docker environment.

Why Clean Up Unused Containers? 

Free Up Resources: Every container, even if stopped, utilizes some system resources like disk space for its filesystem. Cleaning them up can help reclaim these resources. 

Reduce Clutter: Regular maintenance ensures a more organized workspace, aiding in quicker troubleshooting and effective management. 

Minimize Conflicts: Old containers might have settings or network configurations that could conflict with new deployments. 

List All Containers 

Before diving into removal, it's beneficial to first list all containers, both running and stopped.

$ docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED       STATUS                     PORTS     NAMES
a1b2c3d4e5f6   my_app:v1      "npm start"   3 days ago    Exited (0) 3 days ago                my_app_container_1
f6e5d4c3b2a1   old_service    "/bin/bash"   2 weeks ago   Exited (1) 2 weeks ago              redundant_service

Removing a Specific Container 

To remove a particular container, use the docker rm command followed by the container ID or name.

$ docker rm a1b2c3d4e5f6
a1b2c3d4e5f6

This output confirms the deletion of the specified container.

Filtering Containers 

Docker provides powerful filtering options to list containers based on specific criteria: 

By Status: 

To list all containers with an "Exited" status:
$ docker ps -a -f status=exited
CONTAINER ID   IMAGE       COMMAND     CREATED       STATUS                   NAMES
f6e5d4c3b2a1   old_service "/bin/bash" 2 weeks ago   Exited (1) 2 weeks ago   redundant_service

By Name: 

To list all containers with a name containing "app":

$ docker ps -a -f name=app
CONTAINER ID   IMAGE       COMMAND     CREATED       STATUS                  NAMES
a1b2c3d4e5f6   my_app:v1   "npm start" 3 days ago    Exited (0) 3 days ago   my_app_container_1

By Time since Creation: 

To list all containers created more than an hour ago:
$ docker ps -a -f "created=1h"
CONTAINER ID   IMAGE       COMMAND     CREATED        STATUS                  NAMES
f6e5d4c3b2a1   old_service "/bin/bash" 2 weeks ago    Exited (1) 2 weeks ago  redundant_service

Automatically Remove Unused Containers 

For a more efficient cleanup, Docker offers a utility to prune all stopped containers:

docker container prune

For example with output:

$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

Deleted Containers:
a1b2c3d4e5f6, f6e5d4c3b2a1, ...

Total reclaimed space: 600MB

The output lists all the containers that were deleted and the space reclaimed by the operation.

What About Containers with a 'Dead' or 'Exited' Status? 

Containers with statuses like 'Dead' or 'Exited' are considered stopped. Hence, the docker container prune command will remove them. If you wish to restart or troubleshoot such containers instead of removing them, ensure you've reviewed them before pruning.

Conclusion

Regularly cleaning up unused Docker containers is a critical aspect of Docker hygiene. Along with direct removal, Docker's filtering options can help in targeted cleanups, ensuring optimal resource utilization and a clutter-free workspace. Always approach container removal with caution, ensuring you aren't inadvertently deleting something crucial. Here's to a tidy and efficient Docker environment!

Related Container Management Guides

Comments