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
This question is related to
docker
containers
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
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>
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 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)
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
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
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>
.
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!
Source: Stackoverflow.com