program story

종속 자식 이미지가있는 Docker 이미지를 삭제할 수 없습니다.

inputbox 2020. 8. 16. 20:14
반응형

종속 자식 이미지가있는 Docker 이미지를 삭제할 수 없습니다.


나는 노력하고있다

docker rmi c565603bc87f

오류:

데몬의 오류 응답 : 충돌 : c565603bc87f를 삭제할 수 없습니다 (강제 할 수 없음)-이미지에 종속 하위 이미지가 있습니다.

그래서 -f 플래그로도 이미지를 삭제할 수 없습니다. 이미지와 모든 자식을 삭제하는 방법은 무엇입니까?

Linux 및 Docker 버전 :

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

docker 버전 클라이언트 : 버전 : 1.11.2 API 버전 : 1.23 Go 버전 : go1.5.4 Git 커밋 : b9f10c9 빌드 : Wed Jun 1 22:00:43 2016 OS / Arch : linux / amd64

서버 : 버전 : 1.11.2 API 버전 : 1.23 Go 버전 : go1.5.4 Git 커밋 : b9f10c9 빌드 : 6 월 1 일 수요일 22:00:43 2016 OS / Arch : linux / amd64


이미지를 제거하기 전에 불필요한 이미지를 제거해야합니다.

docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

그 후 다음을 실행하십시오.

docker rmi c565603bc87f

어떤 경우에는 (제 경우와 같이) 존재하지 않는 여러 태그가있는 이미지 ID를 지정하여 이미지 를 삭제하려고 할 수 있습니다. 이 중 일부는 다른 이미지에서 사용할 수 있습니다. 이 경우 이미지를 제거하지 않을 수 있습니다 .

여기에 설명 된 중복 태그가있는 경우 제거하려는 중복 태그 대신 docker rmi <image_id>사용 docker rmi <repo:tag>하십시오.


다음을 사용하여 문제의 이미지 뒤에 생성 된 모든 이미지의 이미지 ID와 상위 ID를 찾습니다.

docker inspect --format='{{.Id}} {{.Parent}}' $(docker images --filter since=<image_id> -q)

그런 다음 명령을 호출합니다.

docker rmi {sub_image_id} 

"sub_image_id"는 종속 이미지의 ID입니다.


이전의 모든 답변은 정확하지만 여기에 모든 이미지를 강제로 삭제 하는 한 가지 해결책이 있습니다 (이 명령을 사용하면 모든 이미지가 삭제됩니다 )

docker rmi $(docker images -q) -f

여기에 이미지 설명 입력


나에게 일한 것은 IMAGE ID가 아닌 REPOSITORY : TAG 조합을 사용하는 것이 었습니다.

docker rmi <IMAGE ID>이 이미지와 연결된 컨테이너가없는 명령 으로 도커 이미지를 삭제하려고 할 때 다음 메시지가 표시되었습니다.

$ docker rmi 3f66bec2c6bf
Error response from daemon: conflict: unable to delete 3f66bec2c6bf (cannot be forced) - image has dependent child images

명령을 사용하면 성공적으로 삭제할 수 있습니다. docker rmi RPOSITORY:TAG

$ docker rmi ubuntu:18.04v1
Untagged: ubuntu:18.04v1

이 명령은 모든 이미지를 제거합니다 (주의해서 사용)

--force를 사용해 보셨습니까?

sudo docker rmi $(sudo docker images -aq) --force

이 위의 코드는 매력처럼 실행됩니다.


다음은 이미지와 그에 의존하는 모든 이미지를 제거하는 스크립트입니다.

#!/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

여기서 답은 모든 자손을 찾는 것입니다. 여기에 답이 있습니다.

docker 종속 자식 이미지 목록을 어떻게 얻을 수 있습니까?

그런 다음이를 사용하여 하위 이미지를 순서대로 제거하십시오.


여기 에 Simon Brady의 무차별 대입 방법을 기반으로 구축 하면 이미지가 많지 않은 경우 다음 셸 기능을 사용할 수 있습니다.

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


# docker rm $(docker ps -aq)

After that use the command as Nguyen suggested.


When i want to remove some unused image with name "<none>" in docker i face with the problem unable to delete a354bbc7c9b7 (cannot be forced) - image has dependent child images.So for solving this problem:

sudo docker ps -a

CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                         PORTS                                              NAMES
01ee1276bbe0        lizard:1                    "/bin/sh -c 'java ..."   About an hour ago   Exited (1) About an hour ago                                                      objective_lewin
49d73d8fb023        javaapp:latest              "/usr/bin/java -ja..."   19 hours ago        Up 19 hours                    0.0.0.0:8091->8091/tcp                             pedantic_bell
405fd452c788        javaapp:latest              "/usr/bin/java -ja..."   19 hours ago        Created                                                                           infallible_varahamihira
532257a8b705        javaapp:latest              "/usr/bin/java -ja..."   19 hours ago        Created                                                                           demo-default
9807158b3fd5        javaapp:latest              "/usr/bin/java -ja..."   19 hours ago        Created                                                                           xenodochial_kilby
474930241afa        jenkins                     "/bin/tini -- /usr..."   13 days ago         Up 4 days                      0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   myjenkins
563d8c34682f        mysql/mysql-server:latest   "/entrypoint.sh my..."   3 weeks ago         Up 4 days (healthy)            0.0.0.0:3306->3306/tcp, 33060/tcp                  mymysql
b4ca73d45d20        phpmyadmin/phpmyadmin       "/run.sh phpmyadmin"     4 weeks ago         Exited (0) 3 weeks ago                                                            phpmyadmin

you can see that i have several Images with name javaapp:latest and different container name. So, i killed and remove all container of "javaapp:latest" container with:

sudo docker stop "containerName"

sudo docker rm "containrName"

Then

sudo docker rmi -f "imageId"

So i can remove all the images with name "<none>"

goodluck


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


I had this issue and none of the short answers here worked, even in the page mentioned by @tudor above. I thought I would share here how I got rid of the images. I came up with the idea that dependent images must be >= the size of the parent image, which helps identify it so we can remove it.

I listed the images in size order to see if I could spot any correlations:

docker images --format '{{.Size}}\t{{.Repository}}\t{{.Tag}}\t{{.ID}}' | sort -h -r | column -t

What this does, is use some special formatting from docker to position the image size column first, then run a human readable sort in reverse order. Then I restore the easy-to-read columns.

Then I looked at the <none> containers, and matched the first one in the list with a similar size. I performed a simple docker rmi <image:tag> on that image and all the <none> child images went with it.

The problem image with all the child images was actually the damn myrepo/getstarted-lab image I used when I first started playing with docker. It was because I had created a new image from the first test image which created the chain.

Hopefully that helps someone else at some point.


Suppose we have a Dockerfile

FROM ubuntu:trusty
CMD ping localhost

We build image from that without TAG or naming

docker build .

Now we have a success report "Successfully built 57ca5ce94d04" If we see the docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              57ca5ce94d04        18 seconds ago      188MB
ubuntu              trusty              8789038981bc        11 days ago         188MB

We need to first remove the docker rmi 57ca5ce94d04

Followed by

docker rmi 8789038981bc

By that image will be removed!

A forced removal of all as suggested by someone

docker rmi $(docker images -q) -f

Expanding on the answer provided by @Nguyen - this function can be added to your .bashrc etc and then called from the commandline to help clean up any image has dependent child images errors...

You can run the function as yourself, and if a docker ps fails, then it will run the docker command with sudo and prompt you for your password.

Does NOT delete images for any running containers!

docker_rmi_dependants ()                                                                                                                                                         
{ 
  DOCKER=docker
  [ docker ps >/dev/null 2>&1 ] || DOCKER="sudo docker"

  echo "Docker: ${DOCKER}"

  for n in $(${DOCKER} images | awk '$2 == "<none>" {print $3}');
  do  
    echo "ImageID: $n";
    ${DOCKER} inspect --format='{{.Id}} {{.Parent}}' $(${DOCKER} images --filter since=$n -q);
  done;

  ${DOCKER} rmi $(${DOCKER} images | awk '$2 == "<none>" {print $3}')
}

I also have this in my .bashrc file...

docker_rm_dangling ()  
{ 
  DOCKER=docker
  [ docker ps >/dev/null 2>&1 ] || DOCKER="sudo docker"

  echo "Docker: ${DOCKER}"

  ${DOCKER} images -f dangling=true 2>&1 > /dev/null && YES=$?;                                                                                                                  
  if [ $YES -eq 1 ]; then
    read -t 30 -p "Press ENTER to remove, or CTRL-C to quit.";
    ${DOCKER} rmi $(${DOCKER} images -f dangling=true -q);
  else
    echo "Nothing to do... all groovy!";
  fi  
}

Works with:

$ docker --version 
Docker version 17.05.0-ce, build 89658be

you can just do this:

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


Force deleting a list of images (exclude 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


Image Layer: Repositories are often referred to as images or container images, but actually they are made up of one or more layers. Image layers in a repository are connected together in a parent-child relationship. Each image layer represents changes between itself and the parent layer.

The docker building pattern uses inheritance. It means the version i depends on version i-1. So, we must delete the version i+1 to be able to delete version i. This is a simple dependency.

If you wanna delete all images except the last one (the most updated) and the first (base) then we can export the last (the most updated one) using docker save command as below.

docker save -o <output_file> <your_image-id> | gzip <output_file>.tgz

Then, now, delete all the images using image-id as below.

docker rm -f <image-id i> | docker rm -f <image i-1> | docker rm -f <image-id i-2> ... <docker rm -f <image-id i-k> # where i-k = 1

이제 아래와 같이 저장된 tgz 이미지를로드합니다.

gzip -c <output_file.tgz> | docker load

docker ps -q를 사용하여로드 된 이미지의 이미지 ID를 확인합니다. 태그와 이름이 없습니다. 아래와 같이 간단히 태그와 이름을 업데이트 할 수 있습니다.

docker tag <image_id> group_name/name:tag

참고 URL : https://stackoverflow.com/questions/38118791/can-t-delete-docker-image-with-dependent-child-images

반응형