Can’t delete image with children

2019-02-02 21:54发布

I am trying

docker rmi c565603bc87f

Error:

Error response from daemon: conflict: unable to delete c565603bc87f (cannot be forced) - image has dependent child images

So i can't delete image even with -f flag. How to delete image then and all of its children ?

Linux and docker version:

uname -a Linux goracio-pc 4.4.0-24-generic #43-Ubuntu SMP Wed Jun 8 19:27:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

docker version Client: Version: 1.11.2 API version: 1.23 Go version: go1.5.4 Git commit: b9f10c9 Built: Wed Jun 1 22:00:43 2016 OS/Arch: linux/amd64

Server: Version: 1.11.2 API version: 1.23 Go version: go1.5.4 Git commit: b9f10c9 Built: Wed Jun 1 22:00:43 2016 OS/Arch: linux/amd64

标签: docker
15条回答
我只想做你的唯一
2楼-- · 2019-02-02 22:35

you can just do this:

➜ ~ sudo docker rmi 4ed13257bb55 -f Deleted: sha256:4ed13257bb5512b975b316ef482592482ca54018a7728ea1fc387e873a68c358 Deleted: sha256:4a478ca02e8d2336595dcbed9c4ce034cd15f01229733e7d93a83fbb3a9026d3 Deleted: sha256:96df41d1ce6065cf75d05873fb1f9ea9fed0ca86addcfcec7722200ed3484c69 Deleted: sha256:d95efe864c7096c38757b80fddad12819fffd68ac3cc73333ebffaa42385fded

查看更多
甜甜的少女心
3楼-- · 2019-02-02 22:36

Force deleting a list of images (exclure version 10, for example)

docker images | grep version | grep -v version10 > images.txt && for img in $( awk -F" " '{print $3}' /root/images.txt ) ; do docker rmi -f $img; done

查看更多
走好不送
4楼-- · 2019-02-02 22:38

The answer here is to find all descendent children, which has an answer here:

docker how can I get the list of dependent child images?

Then use that to remove the child images in order.

查看更多
Melony?
5楼-- · 2019-02-02 22:40

Building on Simon Brady's brute force method here, if you don't have a ton of images you can use this shell function:

recursive_remove_image() {
  for image in $(docker images --quiet --filter "since=${1}")
  do
    if [ $(docker history --quiet ${image} | grep ${1}) ]
    then
      recursive_remove_image "${image}"
    fi
  done
  echo "Removing: ${1}"
  docker rmi -f ${1}
}

and then call it using recursive_remove_image <image-id>.

查看更多
Root(大扎)
6楼-- · 2019-02-02 22:42

Here's a script to remove an image and all the images that depend on it.

#!/bin/bash

if [[ $# -lt 1 ]]; then
    echo must supply image to remove;
    exit 1;
fi;

get_image_children ()
{
    ret=()
    for i in $(docker image ls -a --no-trunc -q); do
        #>&2 echo processing image "$i";
        #>&2 echo parent is $(docker image inspect --format '{{.Parent}}' "$i")
        if [[ "$(docker image inspect --format '{{.Parent}}' "$i")" == "$1" ]]; then
            ret+=("$i");
        fi;
    done;
    echo "${ret[@]}";
}

realid=$(docker image inspect --format '{{.Id}}' "$1")
if [[ -z "$realid" ]]; then
    echo "$1 is not a valid image.";
    exit 2;
fi;
images_to_remove=("$realid");
images_to_process=("$realid");
while [[ "${#images_to_process[@]}" -gt 0 ]]; do
    children_to_process=();
    for i in "${!images_to_process[@]}"; do
        children=$(get_image_children "${images_to_process[$i]}");
        if [[ ! -z "$children" ]]; then
            # allow word splitting on the children.
            children_to_process+=($children);
        fi;
    done;
    if [[ "${#children_to_process[@]}" -gt 0 ]]; then
        images_to_process=("${children_to_process[@]}");
        images_to_remove+=("${children_to_process[@]}");
    else
        #no images have any children. We're done creating the graph.
        break;
    fi;
done;
echo images_to_remove = "$(printf %s\n "${images_to_remove[@]}")";
indices=(${!images_to_remove[@]});
for ((i="${#indices[@]}" - 1; i >= 0; --i)) ; do
    image_to_remove="${images_to_remove[indices[i]]}"
    if [[ "${image_to_remove:0:7}" == "sha256:" ]]; then
        image_to_remove="${image_to_remove:7}";
    fi
    echo removing image "$image_to_remove";
    docker rmi "$image_to_remove";
done
查看更多
迷人小祖宗
7楼-- · 2019-02-02 22:43

I also got this issue, I could resolve issue with below commands. this may be cause, the image's container is running or exit so before remove image you need to remove container

docker ps -a -f status=exited : this command shows all the exited containers so then copy container Id and then run below commands to remove container

docker rm #containerId : this command remove container this may be issue that mention "image has dependent child images"

Then try to remove image with below command

docker rmi #ImageId

查看更多
登录 后发表回答