Reputation: 4130
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?
Upvotes: 376
Views: 304986
Reputation: 10224
Perhaps the volume was created via docker compose
? If so, remove it with:
docker compose down --volumes
while in the same directory as your docker-compose.yml
or docker-compose.yaml
file.
Note that this will permanently delete all volumes (as well as containers/networks) defined in the configuration file.
Credit to Niels Bech Nielsen!
Upvotes: 551
Reputation: 1650
When using Docker Desktop and having extensions installed, they can have a docker volume without any container running.
Example:
➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
➜ ~ docker volume ls
DRIVER VOLUME NAME
local mochoa_pgadmin4-docker-extension-desktop-extension_pgadmin_data
➜ ~ docker volume rm mochoa_pgadmin4-docker-extension-desktop-extension_pgadmin_data
Error response from daemon: remove mochoa_pgadmin4-docker-extension-desktop-extension_pgadmin_data: volume is in use - [3a453e427e63138c7f52168004805ebd1728d83fcdf242bbce97c2951bd0eadc]
To solve this, open the Docker Desktop and uninstall the extension.
Upvotes: -1
Reputation: 351
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>
Upvotes: 25
Reputation: 311
First prune the unwanted containers with the command:
docker container prune
(Make sure that you really want to remove your all containers)
After removing all the unwanted containers prune the volume as well using:
docker volume prune
Upvotes: 21
Reputation: 5975
You must remove the container that uses that volume first. List all containers by name, even the existed ones:
docker ps --all --format '{{.Names}}'
Remove a container:
docker rm NAME
After you removed the container you may remove the volume as well. List the volumes:
docker volume ls
Remove the volume:
docker volume remove VOLUME_NAME
Upvotes: 5
Reputation: 186
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.
Upvotes: 4
Reputation: 12420
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
Upvotes: 329
Reputation: 366
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
Upvotes: 13
Reputation: 3265
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.
Upvotes: 261
Reputation: 869
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]$
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.
Upvotes: 72
Reputation: 95
You should type this command with flag -f (force):
sudo docker volume rm -f <VOLUME NAME>
Upvotes: -4
Reputation: 1370
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
Upvotes: 3