16

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?

Uri Lukach
  • 1,093
  • 1
  • 14
  • 28

8 Answers8

17

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

niglesias
  • 437
  • 7
  • 16
Spangen
  • 4,420
  • 5
  • 37
  • 42
  • Thanks! is it still okay to use docker rmi ubuntu --force? – Uri Lukach Oct 30 '17 at 09:45
  • 1
    TBH I don't know the definitive answer to this. I'm like you and don't like to use force unless absolutely necessary. This answer goes into more detail about forced removal: https://stackoverflow.com/questions/21398087/how-can-i-delete-dockers-images – Spangen Oct 30 '17 at 09:49
8

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.

Keilaron
  • 437
  • 4
  • 10
3

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
BMitch
  • 231,797
  • 42
  • 475
  • 450
3

Remove all containers and after all images to win some space !

$ docker rm $(docker ps -all -q)
$ docker rmi $(docker image ls -q)
GuanacoBE
  • 442
  • 1
  • 7
  • 12
2

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
Stefano
  • 4,730
  • 1
  • 20
  • 28
2

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

anrajme
  • 787
  • 8
  • 6
2

When would one delete docker images

  1. When they are short of disk space and
  2. 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

pr-pal
  • 3,248
  • 26
  • 18
2

These are the basic useful commands for docker to view and delete stuff.

  1. View all docker images: docker image ls

  2. View docker containers: docker ps

  3. Stop docker container: docker stop <container name>

  4. Remove docker container: docker rm <container name>

  5. Remove docker image: docker rmi image <image name>

  6. Delete Docker images except for current one - docker image prune -a

Dasun_96
  • 173
  • 1
  • 7