266

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

Mark Chorley
  • 2,087
  • 2
  • 22
  • 29
Yuday
  • 4,171
  • 4
  • 16
  • 23

12 Answers12

297

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
Ahmad Abdelghany
  • 11,983
  • 5
  • 41
  • 36
  • 6
    I ended up having to use `docker rmi -f id` to "force" the removal. – WernerCD Dec 21 '17 at 03:49
  • @WernerCD `-f` helps when you want to remove an image that is referenced in multiple repositories which is another common reason for failing to remove an image. – Ahmad Abdelghany Dec 21 '17 at 12:55
  • 1
    worth noting that `docker container ls` only lists running containers. This messed with me until I realised that adding the `-a` flag lists all of them. – MFB Feb 06 '20 at 01:31
  • @MFB Good point. Added the flag to the command to maintain parity between the two sets of commands. Thanks! – Ahmad Abdelghany Feb 12 '20 at 11:38
  • still works on OSX Docker Desktop v2.2.0.4 (engine 19.03.8) – rrrrrraul Apr 05 '20 at 06:27
  • As you said, the container is the running instance of an image. So, why we cannot remove the container? What is the relation between an image an a running container? – Mahdi Amrollahi Oct 16 '20 at 17:40
  • 1
    @MahdiAmrollahi You can remove a stopped container at anytime with `docker rm `. If a container is still running, stop it first, or force remove with `docker rm -f ` – Ahmad Abdelghany Oct 19 '20 at 09:20
  • 1
    Now that I think about it, the term "running instances" isn't very accurate because containers have their own lifecycle (i.e. can be started/stopped). I will use the term "runnable instances" instead. – Ahmad Abdelghany Oct 19 '20 at 09:27
  • docker system prune -a did the job for me!!! thanks – Zorox May 03 '21 at 01:40
99

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)
Yuday
  • 4,171
  • 4
  • 16
  • 23
  • 6
    If you happen to have a lot of old containers, you might also consider docker `rm \`docker ps -aq\`` for cleanup, rather than removing them one at a time. More information [here](http://stackoverflow.com/questions/17236796/how-to-remove-old-docker-containers?page=1&tab=active#tab-top) – Philiiiiiipp Jan 15 '16 at 10:56
  • Why is it necessary to delete the name before deleting the image? Why should trying to delete an image w/o first deleting the name be an issue at all? – code_dredd Apr 04 '18 at 22:57
  • @code_dredd There is no such thing as "deleting names". It is the container that must be deleted first before deleting an image. More details at https://stackoverflow.com/a/40283776/1998422 – Ahmad Abdelghany Sep 25 '18 at 20:22
51

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)
quAnton
  • 776
  • 6
  • 10
  • That worked. Also, if you stop the container, you will still see it in a list. Therefore, you have to remove it with `docker rm $(docker ps -a -q)`. Thanks! –  May 31 '18 at 17:46
39

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

Saril Sudhakaran
  • 1,109
  • 9
  • 17
  • 1
    I've tried it, by stopping container $ sudo docker stop training/webapp Error response from daemon: no such id: training/webapp Error: failed to stop containers: [training/webapp] – Yuday Nov 25 '15 at 03:24
  • you can try stopping with the container ID `sudo docker stop 0bd2b54678c7 ` – Saril Sudhakaran Nov 25 '15 at 03:40
23

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

gael
  • 769
  • 6
  • 16
  • This worked for me. After I ran this command, I was to then run `docker image ls` and then `docker image rm X` where each X was obtained from the `ls` output. – jftuga Feb 16 '19 at 15:31
12

1-Stop running container:

docker stop <container-id>

2-Remove container

docker rm <container-id>

3-Remove docker image

docker rmi <image-id>
Ahmet Arslan
  • 5,380
  • 2
  • 33
  • 35
  • This is it. Pretty straightforward :) One can use command = "docker ps -a" to list the container ID and "docker images" command to see the list of docker images. Quotes are just indicating command and don't include them while running command. – Badri Paudel Mar 02 '22 at 16:12
8

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>
Mohamad Al Mdfaa
  • 985
  • 1
  • 11
  • 16
5

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

User3301
  • 95
  • 3
  • 8
3

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
Jinna Balu
  • 6,747
  • 38
  • 47
2

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!

keikai
  • 14,085
  • 9
  • 49
  • 68
Anthony Piñero
  • 616
  • 5
  • 10
0

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
AndrewD
  • 4,924
  • 3
  • 30
  • 32
0

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>