Docker: Error response from daemon: remove myvol: volume is in use - docker

When I'm trying to remove a volume I get this error:
Error response from daemon: remove myvol: volume is in use -
[2a177cb40a405db9f245fccd776dcdeacc d266ad624daf7cff510c9a1a1716fe]
But both docker ps and docker container ls return an empty list.
I've tried restarting the docker daemon.
I use Docker Toolbox on Windows 10.

try to delete all stopped containers:
docker rm -f $(docker ps -a -q)
then delete the volume
you can see stopped container using docker ps -a using docker ps will return only running containers
EDIT since you are on Windows
list stopped containers:
docker ps -a
delete the stopped container - you need to replace CONATINER_ID with your real ones -:
docker rm -f CONATINER_ID_1 CONATAINER_ID_2

To fix this error, you need to remove the container [2a177cb40a405db9f245fccd776dcdeacc d266ad624daf7cff510c9a1a1716fe] before removing the volume.
You can use this ID to remove the container:
$ docker rm 2a177cb40a405db9f245fccd776dcdeacc d266ad624daf7cff510c9a1a1716fe
Note: Removing the container will not affect the volume. The ls command will still list this volume.
$ docker volume ls
After you removed the container, the volume is now free for deletion. so you can use the rm command to delete the Docker volume “[Volume Name]”:
$ docker volume rm [Volume Name]

Related

What is the difference between "docker container prune" vs "docker rm $(docker container ls -aq)"

I'm reading through the Docker documentation and I don't understand the difference between:
docker container prune
and
docker rm $(docker container ls -aq)
Note that in the link, the second command I've listed is docker rm $(docker ps -a -q), but there is no difference between that and what I've written. container ls is just the newer version of the ps command.
It seems that both of these commands remove all stopped containers. Is there more to it than that, or are these just synonyms?
I don't think there is substantial difference. This -a though means list all containers and as a result docker rm ... will also try to remove running containers. This gives the error that you see below:
Error response from daemon: You cannot remove a running container [...] Stop the container before attempting removal or force remove
example:
$ docker container run --rm -itd alpine:latest
0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ec4d7459d35 alpine:latest "/bin/sh" 4 seconds ago Up 1 second jovial_ritchie
$ docker rm $(docker container ls -aq)
Error response from daemon: You cannot remove a running container 0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4. Stop the container before attempting removal or force remove
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
But... difference when --force, -f is used:
In this case, the commands do 2 different things:
docker rm -f ... Forces the removal of a running container (uses SIGKILL) which means that it will remove running containers.
$ docker rm -f $(docker container ls -aq)
0ec4d7459d35
docker container prune -f will remove all stopped containers without asking for confirmation (no [y/N] prompt will be printed).
$ docker container prune -f
Total reclaimed space: 0B
The effects of the two commands are indeed similar, but there are some nuances to consider:
docker container prune can be used with the --filter option.
docker container prune has a synchronous protection that blocks concurrent prune executions on the daemon.
docker container prune attempts to remove only the containers that are not running, instead of trying to delete all containers and relying on the daemon to throw an exception for those that are not stopped, therefore is quicker and does not generate unnecessary error logs in case someone is tracking the daemon logs.
docker container prune builds a report at the end of its execution, providing the reclaimed space. The report is added in daemon.EventsService and implicitly displayed on the screen.
docker container prune is shorter
In the end of this answer I have a question: Why would someone type 15 additional characters to get the same result or worse?
docker system prune -f : to remove all the stopped containers (docker do not touch the running containers)
docker system prune -a : to remove all the stopped containers (docker do not touch the running containers) + unused images
docker rm <container_id> : remove a specific container, it should be stopped before (docker stop <container_id>)

Docker rmi - Is it okay to use --force?

I am rather new to Docker, I have recently started running ubuntu container, and stopped it gracefully a few days later (I do not see it using "docker ps"). When I tried to remove ubuntu image using
docker rmi ubuntu
I got the following error:
Error response from daemon: conflict: unable to remove repository reference "ubuntu" (must force) - container 65c315b169b8 is using its referenced image 747cb2d
60bbe
Can I use "--force" to force a removal of the image,
docker rmi ubuntu --force
Or is there a graceful/safer way to do it?
By default docker ps will only show running containers. You can show the stopped ones using docker ps --all.
You can then remove the container first with docker rm <CONTAINER_ID>
If you want to remove all of the containers, stopped or not, you can achieve this from a bash prompt with
$ docker rm $(docker ps --all -q)
The -q switch returns only the IDs
Since everyone else seemed unwilling to test yet willing to preach, I decided to test this out myself:
I downloaded a new image so I knew it wasn't in use
I ran a new container
I deleted the image using docker rmi --force <name>
Image was only untagged, not deleted
I failed to delete the image using docker rmi --force <ID> as docker rebuked with "image is being used by running container 922a12161de6"
So the results are:
The image gets untagged when you name it, but Docker (at least the version I'm using, 19.03.5 build 633a0ea) is smart enough to not actually delete the layers when they are in use.
As a result, the container continues to run fine as the layers are still there, they're simply untagged. You can docker rmi <ID> or docker images prune (without -a it will only delete "dangling" images, I. E. not including unused ones).
Thus the answer is "yes, but it won't delete containers if that's what you're hoping for", but now you know why.
I'm not satisfied with how most of the other answers tell you to find running containers, however, since they seem to say "list all images" -- why? You're trying to delete a specific image.
Stefano's answer is more accurate but here are some tweaks to it:
imageName=ubuntu
containerIds=$(docker ps -a | grep "$imageName" | awk '{ print $1 }')
docker stop $containerIds
docker rm $containerIds
docker rmi "$imageName"
Basically, I added a variable for the image naem and a stop step.
Docker doesn't copy the image data to each container, all the containers running the image have a read only pointer to that part of the filesystem (with their own local RW layer for the individual containers). So if you delete an image while a container is using it, you would break the container and overlay filesystem that it depends on.
Instead, just remove the container first. It may be exited rather than running, so you can do a docker ps -a | grep $image_id for a quick list of containers running that specific image id, but the preferred list would include any descendants:
docker rm $(docker ps -aq --filter "ancestor=747cb2d60bbe")
Then you'll be able to run your docker image rm (or docker rmi) command.
Update: If you force remove an image, all docker does is untag it. The image layers still exists on the filesystem until the container is deleted and then you can delete those image layers. E.g.:
/ $ docker run -d --rm busybox tail -f /dev/null
dac68c445371feab453ba3e3fc80efee52043f6b177fd0a71d0b55b38753f2cf
/ $ docker image rm busybox
Error response from daemon: conflict: unable to remove repository reference "busybox" (must force) - container dac68c445371 is using its referenced image 020584afccce
/ $ docker image rm --force busybox
Untagged: busybox:latest
Untagged: busybox#sha256:1303dbf110c57f3edf68d9f5a16c082ec06c4cf7604831669faf2c712260b5a0
/ $ docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 020584afccce 2 weeks ago 1.22MB
Even after deleting the container the layers are still there:
/ $ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dac68c445371 020584afccce "tail -f /dev/null" 52 seconds ago Up 50 seconds brave_yalow
/ $ docker stop dac
dac
/ $ docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 020584afccce 2 weeks ago 1.22MB
But after the container has been removed you can cleanup the layers:
/ $ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
deleted: sha256:020584afccce44678ec82676db80f68d50ea5c766b6e9d9601f7b5fc86dfb96d
deleted: sha256:1da8e4c8d30765bea127dc2f11a17bc723b59480f4ab5292edb00eb8eb1d96b1
Total reclaimed space: 1.22MB
If you want to do it gracefully, you should find if there are other images using ubuntu. Anyway in your case, you have a container related to that image.
Here's an example script on how to get this:
containerId=$( docker container ls -a | grep ubuntu | awk '{ print $1 }' )
docker container rm $containerId
docker image rm ubuntu
Remove all containers and after all images to win some space !
$ docker rm $(docker ps -all -q)
$ docker rmi $(docker image ls -q)
You can try the prune option available with docker images in case you want to remove all unused images.
https://docs.docker.com/engine/reference/commandline/image_prune/#usage
docker image prune -a
When would one delete docker images
When they are short of disk space and
They know for certain that they dont need an image or if they can readily download from internet/docker registry later when they need
So if you have internet access and if you have disk space issues you can just delete the image by force
Why you are getting the error:
Docker thinks there is a container - which is currently stopped - which was using this image. If you delete this stopped docker, "docker rmi" would work without force
These are the basic useful commands for docker to view and delete stuff.
View all docker images: docker image ls
View docker containers: docker ps
Stop docker container: docker stop <container name>
Remove docker container: docker rm <container name>
Remove docker image: docker rmi image <image name>
Delete Docker images except for current one - docker image prune -a

How do I find a conflicting container that docker reports is not running?

When I start a docker image with a specified name docker reports the name has already been taken by an already running docker container. I cannot find that container by looking at docker ps -a, nor can I remove it referencing it by the id or name. How can I find the conflicting running container? How do I debug this situation futher?
myuser#myhostname$ docker --version
Docker version 1.11.2, build b9f10c9
myuser#myhostname$ docker run --name=myimage-build ubuntu
docker: Error response from daemon: Conflict. The name "/myimage-build" is already in use by container 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.
myuser#myhostname$ docker ps -a | grep 9467
myuser#myhostname$
myuser#myhostname$ docker rm -f 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e
Error response from daemon: No such container: 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e
myuser#myhostname$ docker rm -f myimage-build
Error response from daemon: No such container: myimage-build
myuser#myhostname$
myuser#myhostname$ docker run --name=myimage-build ubuntu
docker: Error response from daemon: Conflict. The name "/myimage-build" is already in use by container 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.
Rao
I work in the same team as Wojtek.
I run the docker inspect you suggested:
docker inspect --format='{{.Name}}' $(docker ps -aq --no-trunc)
/an-unrelated-app
Doesn't seem to be there.
I use several docker-compose yamls with the same name of certains services, and deal with this problem every time.
for me, the fast way to avoid this problem is clean all containers.
docker rm $(docker container ls --all -q)

Docker error cannot delete docker container, conflict: unable to remove repository reference

I want to remove the container at Docker, but an error occurs when you want to delete
My next step before removing the container, see the list of existing container
sts#Yudi:~/docker$ sudo docker ps -as
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
78479ffeba5c ubuntu "/bin/bash" 42 hours ago Exited (0) 42 hours ago sharp_wescoff 81 B (virtual 187.7 MB)
0bd2b54678c7 training/webapp "python app.py" 5 days ago Exited (0) 5 days ago backstabbing_ritchie 0 B (virtual 323.7 MB)
0adbc74a3803 training/webapp "python app.py" 5 days ago Exited (143) 5 days ago drunk_feynman 0 B (virtual 323.7 MB)
one I want to delete the list, namely "training / webapp"
but an error that occurred
sts#Yudi:~/docker$ sudo docker rmi training/webapp
Error response from daemon: conflict: unable to remove repository reference "training/webapp" (must force) - container 0bd2b54678c7 is using its referenced image 54bb4e8718e8
Error: failed to remove images: [training/webapp]
Whether the container is running in the images?
Please help
There is a difference between docker images and docker containers. Check this SO Question.
In short, a container is a runnable instance of an image. which is why you cannot delete an image if there is a running container from that image. You just need to delete the container first.
docker ps -a # Lists containers (and tells you which images they are spun from)
docker images # Lists images
docker rm <container_id> # Removes a stopped container
docker rm -f <container_id> # Forces the removal of a running container (uses SIGKILL)
docker rmi <image_id> # Removes an image
# Will fail if there is a running instance of that image i.e. container
docker rmi -f <image_id> # Forces removal of image even if it is referenced in multiple repositories,
# i.e. same image id given multiple names/tags
# Will still fail if there is a docker container referencing image
Update for Docker 1.13+ [Since Jan 2017]
In Docker 1.13, we regrouped every command to sit under the logical object it’s interacting with
Basically, above commands could also be rewritten, more clearly, as:
docker container ls -a
docker image ls
docker container rm <container_id>
docker image rm <image_id>
Also, if you want to remove EVERYTHING you could use:
docker system prune -a
WARNING! This will remove:
all stopped containers
all networks not used by at least one container
all unused images
all build cache
First, remove the container names
$ sudo docker rm backstabbing_ritchie
The result
$ sudo docker rm backstabbing_ritchie
backstabbing_ritchie
delete the second part, which is listed on the container to be deleted
$ sudo docker rm drunk_feynman
drunk_feynman
Second, remove the container
$ sudo docker rmi training/webapp
The result
$ sudo docker rmi training/webapp
Untagged: training/webapp:latest
Deleted: 54bb4e8718e8600d78a5d7c62208c2f13c8caf0e4fe73d2bc0e474e93659c0b5
Deleted: f74dd040041eb4c032d3025fe38ea85de8075992bdce6789b694a44b20feb8de
Deleted: 7cbae69141977b99c44dc6957b032ad50c1379124d62b7d7d05ab7329b42348e
Deleted: abb991a4ed5e4cde2d9964aec4cccbe0015ba9cc9838b696e7a32e1ddf4a49bd
Deleted: 1952e3bf3d7e8e6a9b1e23bd4142e3c42ff7f4b7925122189704323593fd54ac
Deleted: f95ebd363bf27a7546deced7a41a4099334e37a3d2901fa3817e62bb1ade183f
Deleted: 20dd0c75901396d41a7b64d551ff04952084cc3947e66c67bae35759c80da338
Deleted: 2505b734adda3720799dde5004302f5edb3f2a2ff71438f6488b530b728ba666
Deleted: 2ee0b8f351f753f78f1178000ae37616eb5bf241d4ef041b612d58e1fd2aefdc
Deleted: 2ce633e3e9c9bd9e8fe7ade5984d7656ec3fc3994f05a97d5490190ef95bce8d
Deleted: 98b15185dba7f85308eb0e21196956bba653cf142b36dc08059b3468a01bf35d
Deleted: 515565c29c940355ec886c992231c6019a6cffa17ff1d2abdfc844867c9080c5
Deleted: 2880a3395eded9b748c94d27767e1e202f8d7cb06f1e40e18d1b1c77687aef77
Check the continer
$ sudo docker ps -as
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
78479ffeba5c ubuntu "/bin/bash" 43 hours ago Exited (0) 43 hours ago sharp_wescoff 81 B (virtual 187.7 MB)
If you want to cleanup docker images and containers
CAUTION: this will flush everything
stop all containers
docker stop $(docker ps -a -q)
remove all containers
docker rm $(docker ps -a -q)
remove all images
docker rmi -f $(docker images -a -q)
you can use -f option to force delete the containers .
sudo docker rmi -f training/webapp
You may stop the containers using sudo docker stop training/webapp before deleting
If you have multiples docker containers launched, use this
$ docker rm $(docker ps -aq)
It will remove all the current dockers listed in the "ps -aq" command.
Source : aaam on https://github.com/docker/docker/issues/12487
1-Stop running container:
docker stop <container-id>
2-Remove container
docker rm <container-id>
3-Remove docker image
docker rmi <image-id>
list all your docker images:
docker images
list all existed docker containers:
docker ps -a
delete all the targeted containers, which is using the image that you want to delete:
docker rm <container-id>
delete the targeted image:
docker rmi <image-name:image-tag or image-id>
Noticed this is a 2-years old question, but still want to share my workaround for this particular question:
Firstly, run docker container ls -a to list all the containers you have and pinpoint the want you want to delete.
Secondly, delete the one with command docker container rm <CONTAINER ID> (If the container is currently running, you should stop it first, run docker container stop <CONTAINER ID> to gracefully stop the specified container, if it does not stop it for whatever the reason is, alternatively you can run docker container kill <CONTAINER ID> to force shutdown of the specified container).
Thirdly, remove the container by running docker container rm <CONTAINER ID>.
Lastly you can run docker image ls -a to view all the images and delete the one you want to by running docker image rm <hash>.
Deleting "network not found" in docker
Inspect the network which we are unable to delete
docker network inspect [<id> or <name>]
Disconnect the network
docker network disconnect -f [<networkID> or <networkName>] [<endpointName> or <endpointId>]
Delete unused networks
docker network prune
Remove docker images >
List all containers
docker container ls
List all images
docker image ls
Stop container by container id
docker container stop <container_id>
Remove container by container id
docker container rm <container_id>
If don't want stop and remove, can force remove
docker container rm -f <container_id>
Remove image
docker image rm <image_id>
Done!
Remove just the containers associated with a specific image
docker ps -a | grep training/webapp | cut -d ' ' -f 1 | xargs docker rm
ps -a: list all containers
grep training/webapp : filter out everything but the containers started from the training/webapp image
cut -d ' ' -f 1: list only the container ids (first field when delimited by space)
xargs docker rm : send the container id list output to the docker rm command to remove the container
Simply these two commands solve my issue.
Stop the particular container.
docker stop <container_id>
Forces removal of the image even if it is referenced in multiple repositories.
docker rmi -f <container_id>

Unable to stop or remove a container in Docker, Permission denied is displayed

I have run several containers from an image in different ports, but I cant stop or remove these containers. I have run this command:
# sudo docker rm -f f85956145f61
And no message are displayed, however container is still displayed.
I have tried with these commands:
# docker stop $(docker ps -a -q)
# docker rm $(docker ps -a -q)
But I get the following error:
# Get http:///var/run/docker.sock/v1.15/containers/json?all=1: dial unix /var/run/docker.sock: permission denied
So, how can I remove all containers from my docker?
I don't know if you are trying to stop the container as root, but if not try to stop/kill the container as root or using sudo.
$ sudo docker kill $(docker ps -q)
$ sudo docker rm $(docker ps -a -q)
If this does not work try restarting docker service
$ sudo service docker restart
and then try again to stop and delete the container. If it doesn't work you can try to kill the process(es) running inside the container from your host machine.
And to reference the container in docker kill, docker stop, docker rm,... and so on, you can either specify the container id or the container name, both are valid.
Another reason for this happening is that the daemon is busy committing the container.

Resources