This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
docker:remove_images_containers_volumes [2020/03/03 13:59] admin |
docker:remove_images_containers_volumes [2020/03/03 14:10] (current) admin |
||
---|---|---|---|
Line 160: | Line 160: | ||
==== Remove containers according to a pattern ==== | ==== Remove containers according to a pattern ==== | ||
- | |||
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 rmi. Note that these utilities are not supplied by Docker and not necessarily available on all systems: | 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 rmi. Note that these utilities are not supplied by Docker and not necessarily available on all systems: | ||
+ | === List: === | ||
+ | <code bash> | ||
+ | docker ps -a | grep "pattern” | ||
+ | </code> | ||
+ | === Remove: === | ||
+ | <code bash> | ||
+ | docker ps -a | grep "pattern" | awk '{print $3}' | xargs docker rmi | ||
+ | </code> | ||
- | List: | + | ==== Stop and remove all containers ==== |
- | docker ps -a | grep "pattern” | + | 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: |
- | Remove: | + | === List: === |
- | + | ||
- | docker ps -a | grep "pattern" | awk '{print $3}' | xargs docker rmi | + | |
- | + | ||
- | Stop and remove all containers | + | |
- | + | ||
- | 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: | + | |
- | List: | + | <code bash> |
+ | docker ps -a | ||
+ | </code> | ||
- | docker ps -a | + | === Remove: === |
- | Remove: | + | <code bash> |
+ | docker stop $(docker ps -a -q) | ||
+ | docker rm $(docker ps -a -q) | ||
+ | </code> | ||
- | docker stop $(docker ps -a -q) | + | ===== Removing Volumes ===== |
- | docker rm $(docker ps -a -q) | + | |
- | Removing Volumes | + | ==== Remove one or more specific volumes - Docker 1.9 and later ==== |
- | Remove one or more specific volumes - Docker 1.9 and later | + | |
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: | 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: | ||
- | List: | + | === List: === |
- | docker volume ls | + | <code bash> |
+ | docker volume ls | ||
+ | </code> | ||
- | Remove: | + | === Remove: === |
- | docker volume rm volume_name volume_name | + | <code bash> |
+ | docker volume rm volume_name volume_name | ||
+ | </code> | ||
- | Remove dangling volumes - Docker 1.9 and later | + | ==== Remove dangling volumes - Docker 1.9 and later ==== |
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: | 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: | ||
- | List: | + | === List: === |
+ | <code bash> | ||
- | docker volume ls -f dangling=true | + | docker volume ls -f dangling=true |
+ | </code> | ||
- | Remove: | + | === Remove: === |
- | docker volume prune | + | <code bash> |
+ | docker volume prune | ||
+ | </code> | ||
- | Remove a container and its volume | + | ==== Remove a container and its volume ==== |
If you created 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. | If you created 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. | ||
- | Remove: | + | === Remove: === |
- | docker rm -v container_name | + | <code bash> |
+ | docker rm -v container_name | ||
+ | </code> | ||
- | Conclusion | + | ===== Conclusion ===== |
This guide covers some of the common commands used to remove images, containers, and volumes with Docker. There are many other combinations and flags that can be used with each. For a comprehensive guide to what’s available, see the Docker documentation for docker system prune, docker rmi, docker rm and docker volume rm. If there are common cleanup tasks you’d like to see in the guide, please ask or make suggestions in the comments. | This guide covers some of the common commands used to remove images, containers, and volumes with Docker. There are many other combinations and flags that can be used with each. For a comprehensive guide to what’s available, see the Docker documentation for docker system prune, docker rmi, docker rm and docker volume rm. If there are common cleanup tasks you’d like to see in the guide, please ask or make suggestions in the comments. |