Docker 如何列出图像及其容器

Docker 如何列出图像及其容器,docker,Docker,我正在删除悬挂的docker图像 在删除这些图像之前,我想看看是否有任何容器,它们是这些悬空图像中的实例 如果是这样,我想记录它们并中止删除 到目前为止,我没有找到任何命令 我的解决方案是获取所有容器docker ps-a和所有悬空图像docker-images-aqf-dangling=true,并将图像中的repo+tag与容器中的image进行比较 我正在使用docker1.12 如何列出图像及其容器 您可以编辑--格式,以满足您的需要: docker ps -a --format="co

我正在删除悬挂的docker图像

在删除这些图像之前,我想看看是否有任何容器,它们是这些悬空图像中的实例

如果是这样,我想记录它们并中止删除

到目前为止,我没有找到任何命令

我的解决方案是获取所有容器
docker ps-a
和所有悬空图像
docker-images-aqf-dangling=true
,并将图像中的
repo+tag
与容器中的
image
进行比较

我正在使用docker
1.12

如何列出图像及其容器

您可以编辑
--格式
,以满足您的需要:

docker ps -a --format="container:{{.ID}} image:{{.Image}}"
如何删除悬挂的图像

此命令用于在不接触容器正在使用的图像的情况下清洁悬挂图像:

$ docker image prune

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
docker ps -a --format="{{.Image}}"

但是如果docker版本中没有该命令,可以尝试以下方法

如果图像悬空,您应该在
docker ps
中的图像列中看到散列。这不应该是通常的情况,很难

这将通过运行/停止容器打印使用过的图像:

$ docker image prune

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
docker ps -a --format="{{.Image}}"
下面列出您的悬挂图像:

docker images -qf "dangling=true"
谨慎行事:

#!/bin/bash

# Remove all the dangling images
DANGLING_IMAGES=$(docker images -qf "dangling=true")
if [[ -n $DANGLING_IMAGES ]]; then
    docker rmi "$DANGLING_IMAGES"
fi

# Get all the images currently in use
USED_IMAGES=($( \
    docker ps -a --format '{{.Image}}' | \
    sort -u | \
    uniq | \
    awk -F ':' '$2{print $1":"$2}!$2{print $1":latest"}' \
))

# Remove the unused images
for i in "${DANGLING_IMAGES[@]}"; do
    UNUSED=true
    for j in "${USED_IMAGES[@]}"; do
        if [[ "$i" == "$j" ]]; then
            UNUSED=false
        fi
    done
    if [[ "$UNUSED" == true ]]; then
        docker rmi "$i"
    fi
done

请参阅docker ps--格式化祖先…docker不允许您删除任何容器正在使用的任何图像。不需要检查,除非你也要先删除那些容器…@user2915097我想你的意思是--filter/-f祖先,但实际上这就是我在删除我的悬挂图像之前要查看的内容我想看看是否有任何容器,这些容器是这些悬挂图像的实例。所以我认为,正如user2915097提到的,运行docker ps-qf祖先=imageID只会给我提供来自悬挂图像的实例的容器。因此,我可以删除所有相关的容器,然后删除图像。对您没有用处
docker system prune
?我不清楚您是否也要删除相关的容器