如何列出图像及其容器

我正在删除晃来晃去的泊坞窗图像。

删除这些图像之前,我想看看是否有任何容器,这些悬挂的图像的实例。

如果是这样我想logging他们并中止删除。

到目前为止,我没有find任何指令。

我的解决scheme将获得所有容器docker ps -a和所有悬挂的图像docker images -aqf dangling=true并比较来自图像的repo + tag和来自容器的image

我正在使用docker1.12

如何列出图像及其容器?

您可以编辑 – --format以适应您的需求:

 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版本中没有该命令,则可以尝试以下操作。

如果图像悬而未决,您应该在docker ps的IMAGE列中看到散列。 这不应该是一个通常的情况,艰难。

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

 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