Stop and Remove docker container & volumes

  1. 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:


    docker ps -a


    docker stop $(docker ps -a -q)

    docker rm $(docker ps -a -q)

  2. Remove all images

    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:


    docker images -a


    docker rmi $(docker images -a -q)

  3. 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, however, 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 add a -q flag to provide the volume name to docker volume rm:


    docker volume ls -f dangling=true


    docker volume rm $(docker volume ls -f dangling=true -q)

  4. 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.


    docker rm -v container_name



Leave a Reply