我在努力

docker rmi c565603bc87f

错误:

来自守护进程的错误响应:冲突:无法删除c565603bc87f (不能强制)- image有依赖的子映像

所以我不能用-f标记删除image。如何删除图像,然后所有的孩子?

Linux和docker版本:

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

码头工人版本 客户: 版本:1.11.2 API版本:1.23 Go版本:go1.5.4 Git提交:b9f10c9 建成:2016年6月1日星期三22:00:43 OS /拱:linux / amd64

服务器: 版本:1.11.2 API版本:1.23 Go版本:go1.5.4 Git提交:b9f10c9 建成:2016年6月1日星期三22:00:43 OS /拱:linux / amd64


当前回答

强制删除图像列表(例如,不包括版本10)

Docker images | grep version | grep -v version10 > images.txt && for img in $(awk -F" " '{print $3}' /root/images.txt);码头工人做rmi吗 - f img美元;完成

其他回答

下面是一个脚本,用于删除一个图像和依赖于它的所有图像。

#!/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图像的标签

docker rmi <rep:tag>

如果你想删除Docker镜像

docker image rm <image_id>

例如:输入docker image ls来显示图像的信息

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              3.6            60f85556d5d2        4 days ago          174MB

Docker rmi python:3.6

Docker镜像rm 60f85556d5d2

在Simon Brady的蛮力方法的基础上,如果你没有大量的图像,你可以使用这个shell函数:

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

然后使用recursive_remove_image <image-id>调用它。

在某些情况下(比如我的例子),您可能试图通过指定具有多个您没有意识到存在的标记的图像id来删除图像,其中一些可能被其他图像使用。在这种情况下,您可能不想删除图像。

如果你有一个冗余标签的情况,如这里所述,而不是docker rmi <image_id>使用docker rmi <repo:标签>在你希望删除的冗余标签上。

正如这里所解释的,我使用以下方法来识别依赖图像并删除它们,

image_id=123456789012

docker images -a -q --filter since=$image_id |
xargs docker inspect --format='{{.Id}} {{.Parent}}'

你会看到类似这样的输出:

sha256:f7ef19862215ec0bf7a6b103504d213e1c001691703808f4154689cfbb5f14f9 sha256:a7d2efad2847bd10e5223980ed80f5781c716eddbf6131a3cf97614e7f2db97f
sha256:03690ae141346203959d0ae1b3e8d34b7a4232095d774af57dda6282fce99cc4 sha256:5713074659bb5352496ea680a903eba2f66e0495538c9db37336f4ba92994ea8
sha256:311f587811942d328edc52e5953d794eb9b81fe392512080d9fc1d350a6b2024 sha256:aa674f7f2621946db257720c378377b8714739d20879542d875b84c53b59bc75

然后你可以逐个删除这些图片,如下所示:

docker image rm f7ef19862215ec0bf7a6b103504d213e1c001691703808f4154689cfbb5f14f9

输出类似如下所示的内容:

Untagged: prathap/cognitive_robotics_gpu:v1
Untagged: prathap/cognitive_robotics_gpu@sha256:db6e7543a13e9a96241c985b9b3145b8fd65effb68c183301385b495875f1a5a
Deleted: sha256:03690ae141346203959d0ae1b3e8d34b7a4232095d774af57dda6282fce99cc4
Deleted: sha256:263f655670436758f8e3f23f31170083fc8d60c4eebe01a5b3fda1e73bed3ad1