Unable to delete docker image of a running container using -f option
Asked Answered
H

3

5

I tried to create a container using the following

docker run -it centos bash

and exited without killing it.

Now I'm trying to delete the centos image using its image ID.

docker rmi -f 0f3e07c0138f
Error response from daemon: conflict: unable to delete 0f3e07c0138f (cannot be forced) - image is being used by running container a9eab034a426

On docker documentation page, it says " You cannot remove an image of a running container unless you use the -f option." but still I'm unable to remove image using -f. What can be the explanation for this? Should there a correction in documentation?

Handgun answered 1/1, 2020 at 18:8 Comment(3)
The image provides most of the actual filesystem content for the container; why do you want to do this? Is this a question about programming that you can attach some actual source code to?Switzer
@DavidMaze Container is an instantiation of image with separate R/W layer. If the container is stopped and the image is deleted and the container will work if started again. What I'm asking is why can't we remove it while container is running?Handgun
@harindersingh Container is the top layer ( as you said writable layer ) on an image. It can't exist without other layers. Still, it doesn't answer your question :|Mentalist
M
1

See these results

$ sudo docker rmi centos
Error response from daemon: conflict: unable to remove repository reference 
"centos" (must force) - container be8f69d76892 is using its referenced image 
0f3e07c0138f
$ sudo docker rmi -f 0f3e07c0138f
Error response from daemon: conflict: unable to delete 0f3e07c0138f (cannot be 
forced) - image is being used by running container be8f69d76892
$ sudo docker rmi -f centos
Untagged: centos:latest
Untagged: 
centos@sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9

docker rmi -f option only works on image name if container is running, even then it just untag the image but the image remains there with "none" repo and tag. see

<none>   <none>       0f3e07c0138f        3 months ago        220MB

And when you will run

sudo docker ps

you will see that image id is there instead of the image name

CONTAINER ID        IMAGE                                                
be8f69d76892        0f3e07c0138f

I have removed other info in this docker ps command's result.

Mentalist answered 2/1, 2020 at 20:47 Comment(0)
P
9

a9eab034a426 container is using centos image. So, to remove centos, you need to stop and remove a9eab034a426 container. Run the following lines.

docker stop a9eab034a426
docker rm a9eab034a426
docker rmi 0f3e07c0138f
Protozoan answered 1/1, 2020 at 18:38 Comment(1)
The question is wrt to using -f option. docs.docker.com/engine/reference/commandline/rmi mentions "You cannot remove an image of a running container unless you use the -f option." so technically you should be able to remove image using -f even if container is runningHandgun
M
1

See these results

$ sudo docker rmi centos
Error response from daemon: conflict: unable to remove repository reference 
"centos" (must force) - container be8f69d76892 is using its referenced image 
0f3e07c0138f
$ sudo docker rmi -f 0f3e07c0138f
Error response from daemon: conflict: unable to delete 0f3e07c0138f (cannot be 
forced) - image is being used by running container be8f69d76892
$ sudo docker rmi -f centos
Untagged: centos:latest
Untagged: 
centos@sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9

docker rmi -f option only works on image name if container is running, even then it just untag the image but the image remains there with "none" repo and tag. see

<none>   <none>       0f3e07c0138f        3 months ago        220MB

And when you will run

sudo docker ps

you will see that image id is there instead of the image name

CONTAINER ID        IMAGE                                                
be8f69d76892        0f3e07c0138f

I have removed other info in this docker ps command's result.

Mentalist answered 2/1, 2020 at 20:47 Comment(0)
P
0

in order to delete a Docker image, you need to follow these steps:

  • Get the image ID of the image you want to delete

    docker images | grep $1 | awk '{print $3}'
    
  • Using the obtained image ID list, stop and remove all containers that are using it

     docker ps -a --filter "ancestor=$IMAGE_ID" --format "{{.ID}}"
    
     # Stop the containers
     docker stop $CONTAINER_IDS
     # Remove the containers
     docker rm $CONTAINER_IDS
    
  • Finally, remove the image

    docker rmi -f $IMAGE_ID
    

Here my bash function that automates all steps:

docker_rm_image(){
    if [ -z "$1" ]
    then
        log "usage:"  
        log "   docker_rm <image-name>"
        return 0
    fi
    # Get the list of image IDs with the specific image name
    IMAGE_IDS=$(docker images | grep $1 | awk '{print $3}')
    # If no images found
    if [ -z "$IMAGE_IDS" ]; then
        echo "No images found with the name '$1'"
        return 0
    fi
    #print all images that match the name
    docker images | grep $1
    #ask for confirmation
    read -p "Do you want to remove all images with the name $1? (y/n) " -n 1 -r
    #if response is no then exit
    if [[ ! $REPLY =~ ^[Yy]$ ]]
    then
        echo "Exiting..."
        return 0
    fi
    # Iterate over each image ID
    for IMAGE_ID in $IMAGE_IDS; do
        echo "Find and stop containers using the image"
        CONTAINER_IDS=$(docker ps -a --filter "ancestor=$IMAGE_ID" --format "{{.ID}}")

        # If there are containers using the image
        if [ ! -z "$CONTAINER_IDS" ]; then
            echo "Containers found using the image... stopping and removing them"
            # Stop the containers
            docker stop $CONTAINER_IDS
            # Remove the containers
            docker rm $CONTAINER_IDS
        else
            echo "No containers found using the image"
        fi

        echo "Removing the image with ID $IMAGE_ID"
        docker rmi -f $IMAGE_ID
    done
}
Pastoral answered 20/6 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.