How To Remove Docker Images, Containers, and Volumes
Source: DigitalOcean
By Melissa Anderson and Anish Singh Walia Docker makes it easy to wrap your applications and services in containers so you can run them anywhere. However, as you work with Docker, it’s also easy to accumulate an excessive number of unused images, containers, and data volumes that clutter the output and consume disk space. Docker gives you all the tools you need to clean up your system from the command line. This cheat sheet-style guide provides a quick reference to commands that are useful for freeing disk space and keeping your system organized by removing unused Docker images, containers, and volumes. How to Use This Guide: Note: The command substitution syntax, command $(command), used in the commands is available in many popular shells, such as bash, zsh, and Windows Powershell. If you want a 1-click way to deploy a Docker application to a live server, take a look at DigitalOcean App Platform. Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not tagged or associated with a container): To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command: Use the docker images command with the -a flag to locate the ID of the images you want to remove. This will show you every image, including intermediate image layers. When you’ve located the images you want to delete, you can pass their ID or tag to docker rmi: Note: The -a or --all flag in the docker images command displays all the Docker images, including intermediate ones that are not referenced by any tags. By default, docker images shows only the images with at least one tag. However, there may be some images without any tags that are still taking up disk space on the system. The -a flag can be helpful in identifying images that can be pruned to save disk space. When used with the docker rmi command, the -f or --force flag can also be used to remove images with no tags. Docker images consist of multiple layers. Dangling images are layers that have no relationship to any tagged images. They no longer serve a purpose and consume disk space. They can be located by adding the filter flag -f with a value of dangling=true to the docker images command. When you’re sure you want to delete them, you can use the docker image prune command: Note: If you build an image without tagging it, the image will appear on the list of dangling images because it has no association with a tagged image. You can avoid this situation by providing a tag when you build, and you can retroactively tag an image with the docker tag command. You can find all the images that match a pattern using a combination of docker images and grep. Once you’re satisfied, you can delete them by using awk to pass the IDs to docker rmi. Note that these utilities are not supplied by Docker and are not necessarily available on all systems: All the Docker images on a system can be listed by adding -a to the docker images command. Once you’re sure you want to delete them all, you can add the -q flag to pass the image ID to docker rmi: Use the docker ps command with the -a flag to locate the name or ID of the containers you want to remove: If you know when you’re creating a container that you won’t want to keep it around once you’re done, you can run docker run --rm to automatically delete it when it exits: You can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exited. To review the list of exited containers, use the -f flag to filter based on status. When you’ve verified you want to remove those containers, use -q to pass the IDs to the docker rm command: Docker filters can be combined by repeating the filter flag with an additional value. This results in a list of containers that meet either condition. For example, if you want to delete all containers marked as either created (a state which can result when you run a container with an invalid command) or exited, you can use two filters: You can find all the containers that match a pattern using a combination of docker ps and grep. When you’re satisfied that you have the list you want to delete, you can use awk and xargs to supply the ID to docker rm. Note that these utilities are not supplied by Docker and are not necessarily available on all systems: You can review the containers on your system with docker ps. Adding the -a flag will show all containers. When you’re sure you want to delete them, you can add the -q flag to supply the IDs to the docker stop and docker rm commands: Use the docker volume ls command to locate the volume name or names you wish to delete. Then you can remove one or more volumes with the docker volume rm command: Since the point of volumes is to exist independent from containers, when a container is removed, a volume is not automatically removed at the same time. When a volume exists and is no longer connected to any containers, it’s called a dangling volume. To locate them to confirm you want to remove them, you can use the docker volume ls command with a filter to limit the results to dangling volumes. When you’re satisfied with the list, you can remove them all with docker volume prune: If you create an unnamed volume, it can be deleted at the same time as the container with the -v flag. Note that this only works with unnamed volumes. When the container is successfully removed, its ID is displayed. Note that no reference is made to the removal of the volume. If it is unnamed, it is silently removed from the system. If it is named, it silently stays present. Note: The -f flag is used to force the removal of resources without prompting for confirmation. The -a flag is used to remove all unused resources, including dangling ones. When multiple containers share volumes, thread synchronization issues can arise, leading to data corruption or unexpected behavior. To handle these issues, you can use the following strategies: Implement File Locks: Use file locking mechanisms to ensure that only one container can access a file at a time. This can be achieved using tools like flock or lockfile within your application code. Use Docker Compose: Docker Compose allows you to define and manage multi-container applications, ensuring proper synchronization and volume sharing. Here’s an example of a Docker Compose file that defines a service with a named volume: Excess image layers can lead to performance bottlenecks, especially during the build and deployment process. To debug and resolve these issues, follow these steps: When you try to remove an active container, you may encounter the container is running error. To fix this issue, you have several options: First stop the container, then remove it: Force remove the container in a single command: Stop and remove all containers: Use Docker Compose to stop and remove containers: If you’re using Docker Desktop, you can also use the GUI to stop and remove containers by right-clicking on the running container and selecting “Stop” and then “Remove”. 1. How do I delete all stopped containers in Docker? To delete all stopped containers in Docker, use the following command: This command will stop and remove all containers defined in your docker-compose.yml file. If you want to remove all stopped containers without using docker-compose, you can use: 2. What happens when I run docker system prune? When you run docker system prune, Docker will remove all stopped containers and all networks not used by at least one container. Additionally, if you use the -a flag, Docker will also remove all unused images. This command is useful for freeing up disk space and cleaning up your Docker environment. 3. Can I remove a running Docker container? Yes, you can remove a running Docker container using the -f flag with the docker rm command. This will force the removal of the container without stopping it first. Here’s an example: 4. How do I free up disk space used by Docker? To free up disk space used by Docker, you can use the following commands: 5. What is the difference between docker rm and docker rmi? docker rm is used to remove a container, while docker rmi is used to remove an image. docker rm will delete a container and its associated resources, but it will not delete the image that the container was based on. docker rmi, on the other hand, will delete an image, but it will not delete any containers that are based on that image. 6. How do I completely remove Docker images? To completely remove a Docker image, use the following command: Replace
For now as a punch line you can add the new alternate way : docker system prune
See https://docs.docker.com/engine/reference/commandline/system_prune/
Can’t figure out since this new command released. This was a life saver. I am a rookie at this and it helped greatly. Thanks! What about the build in CLI tools Thanks for the document.
To remove dangling images the command seems to be docker image prune, not docker images purge Nice… A quick read for the essential cmds… An add up, use xargs with *-r * flag.
Otherwise if left hand side doesn’t give any data which will trigger error -r, --no-run-if-empty if there are no arguments, then do not run COMMAND;
if this option is not given, COMMAND will be
run at least once docker images -a | grep “pattern” | awk ‘{print $3}’ | xargs -r docker rmi That’s good, thanks for this complete post, it’s appreciated! Thanks! It resolved my issue! Please complete your information! Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation. Full documentation for every DigitalOcean product. The Wave has everything you need to know about building a business, from raising funding to marketing your product. Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter. New accounts only. By submitting your email you agree to our Privacy Policy Scale up as you grow — whether you're running one virtual machine or ten thousand. Sign up and get $200 in credit for your first 60 days with DigitalOcean.* *This promotional offer applies to new accounts only.