236
votes

EDIT (2/19/21): A lot of time has elapsed since I asked this original question years ago and I've seen a flurry of activity since then. I re-selected an answer which I think is consistent with the most localized and safe option for solving this issue (which is typically associated with docker-compose). While docker did introduce the prune command, it is generally a dangerous operation and I would be cautious about using it as you may unintentionally impact other applications or setups you have on your machine


I've been having issues with removing Docker volumes with Docker 1.9.1.

I've removed all my stopped containers so that docker ps -a returns empty.

When I use docker volume ls, I'm given a whole host of Docker containers:

docker volume ls
DRIVER              VOLUME NAME
local               a94211ea91d66142886d72ec476ece477bb5d2e7e52a5d73b2f2f98f6efa6e66
local               4f673316d690ca2d41abbdc9bf980c7a3f8d67242d76562bbd44079f5f438317
local               eb6ab93effc4b90a2162e6fab6eeeb65bd0e4bd8a9290e1bad503d2a47aa8a78
local               91acb0f7644aec16d23a70f63f70027899017a884dab1f33ac8c4cf0dabe5f2c
local               4932e2fbad8f7e6246af96208d45a266eae11329f1adf176955f80ca2e874f69
local               68fd38fc78a8f02364a94934e9dd3b5d10e51de5b2546e7497eb21d6a1e7b750
local               7043a9642614dd6e9ca013cdf662451d2b3df6b1dddff97211a65ccf9f4c6d47
#etc x 50

Since none of these volumes contain anything important, I try to purge all the volumes with docker volume rm $(docker volume ls -q).

In the process, the majority are removed, but I get back:

Error response from daemon: Conflict: volume is in use
Error response from daemon: Conflict: volume is in use
Error response from daemon: Conflict: volume is in use
Error response from daemon: Conflict: volume is in use
Error response from daemon: Conflict: volume is in use

For a sizeable portion of them. If I don't have any containers existing in the first place, how are these volumes being used?

9
docker uses reference counting to check if a volume is still in use; this is all done in-memory; this may be a bug or a race condition somehow, which resulted in the container being removed, but the counter not being updated. A restart of the daemon should resolve this, but, yes it's possible there's a bug somewhere. Is there something special in your setup (e.g. Are you using docker-in-docker, Swarm?). Do you use some script or tool to cleanup your containers?thaJeztah
Hey thanks @thaJeztah restarting the Docker daemon (sudo service docker stop and sudo service docker start) cleared out all of these ghost volumes for me. Moreover, it seems like I am now able to remove volumes without issue using the docker rm -v command. Only notable differences in usage is that I've been using docker-compose on Ubuntu 15.10. I'll report back if I'm ever able to replicate this problem but otherwise it seems like a simple restart will suffice. Thanks!Tkwon123
even after reboot it still says docker volume is in use..holms
If you use docker compose, you can add -v to the down command, to remove the volumes.Niels Bech Nielsen
I fixed this by stopping docker then removing the volumes from the file system and starting docker again. service docker stop && rm -rf /var/lib/docker/volumes/TheVolumIdYouWantToRemove && service docker startjfgrissom

9 Answers

325
votes

Perhaps the volume was created via docker-compose? If so, it should get removed by:

docker-compose down --volumes

Credit to Niels Bech Nielsen!

219
votes

You can use these functions to brutally remove everything Docker related:

removecontainers() {
    docker stop $(docker ps -aq)
    docker rm $(docker ps -aq)
}

armageddon() {
    removecontainers
    docker network prune -f
    docker rmi -f $(docker images --filter dangling=true -qa)
    docker volume rm $(docker volume ls --filter dangling=true -q)
    docker rmi -f $(docker images -qa)
}

You can add those to your ~/Xrc file, where X is your shell interpreter (~/.bashrc if you're using bash) file and reload them via executing source ~/Xrc. Also, you can just copy paste them to the console and afterwards (regardless the option you took before to get the functions ready) just run:

armageddon

It's also useful for just general Docker clean up. Have in mind that this will also remove your images, not only your containers (either running or not) and your volumes of any kind.

152
votes

Volume can be in use by one of stopped containers. You can remove such containers by command:

docker container prune

then you can remove not used volumes

docker volume prune
65
votes

I am fairly new to Docker. I was cleaning up some initial testing mess and was not able to remove a volume either. I had stopped all the running instances, performed a docker rmi -f $(docker image ls -q), but still received the Error response from daemon: unable to remove volume: remove uuid: volume is in use.

I did a docker system prune and it cleaned up what was needed to remove the last volume:

[0]$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
... about 15 containers UUID's truncated

Total reclaimed space: 2.273MB
[0]$ docker volume ls
DRIVER              VOLUME NAME
local              uuid
[0]$ docker volume rm uuid
uuid
[0]$

docker system prune

The client and daemon API must both be at least 1.25 to use this command. Use the docker version command on the client to check your client and daemon API versions.

10
votes

As long as volumes are associated with a container(either running or not), they cannot be removed.

You have to run

docker inspect <container-id>/<container-name>

on each of the running/non-running containers where this volume might have been mounted onto.

If the volume is mounted onto any one of the containers, you should see it in the Mounts section of the inspect command output. Something like this :-

"Mounts": [
            {
                "Type": "volume",
                "Name": "user1",
                "Source": "/var/lib/docker/volumes/user1/_data",
                "Destination": "/opt",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

After figuring out the responsible container(s), use :-

docker rm -f container-1 container-2 ...container-n in case of running containers

docker rm container-1 container-2 ...container-n in case of non-running containers

to completely remove the containers from the host machine.

Then try removing the volume using the command :-

docker volume remove <volume-name/volume-id>

10
votes

Currently you can use what docker offers now for a general and more complete cleaning:

docker system prune

To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command:

docker system prune -a
3
votes

I am pretty sure that those volumes are actually mounted on your system. Look in /proc/mounts and you will see them there. You will likely need to sudo umount <path> or sudo umount -f -n <path>. You should be able to get the mounted path either in /proc/mounts or through docker volume inspect

0
votes

A one liner to give you just the needed details:

docker inspect `docker ps -aq` | jq '.[] | {Name: .Name, Mounts: .Mounts}' | less

search for the volume of complaint, you have the container name as well.

-3
votes

You should type this command with flag -f (force):

sudo docker volume rm -f <VOLUME NAME>