为什么当构建退出错误时,来自docker文件的docker构建映像会创建容器? [英] Why docker build image from docker file will create container when build exit incorrectly?

查看:125
本文介绍了为什么当构建退出错误时,来自docker文件的docker构建映像会创建容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用docker从docker文件构建映像。在此过程中发生了一些错误,因此生成错误代码的构建退出。

I'm using docker to build images from a docker file. In the process there's some error happened, so the build exit with error code.

当我运行 docker images 时,可以看到未加标签的图像。所以我尝试将其删除 docker rmi xxxxx 。但是它总是失败,它说无法删除图像,因为它已被停止的容器使用。

When I run docker images I can see a untagged image. so I tried to remove it docker rmi xxxxx. But it always fails, it says the images can't be removed because it's used by a stopped container.

因此,我进行了更深入的研究。我运行 docker ps -a ,现在可以看到一长串的停止容器,这些容器是在构建过程失败时创建的。

So I dig a little deeper. I run docker ps -a, now I can see a long list of stopped container which are created when the build process fails.

为什么会创建容器?我认为图像就像编程中类的概念,容器是类的实例。在成功构建图片之前,为什么要创建实例?如何在没有所有那些停止容器的情况下构建映像?

Why there will be container created?? I thought image is like the concept of "class" in programming, and container is instance of the class. Before the image is successfully built, why there'll be instance created? How can I build image without all those stopped containers ?

推荐答案

Dockerfile的每一行都创建一个中间容器来执行Dockerfile指令

Each line of your Dockerfile creates an intermediate container to execute the Dockerfile directive for that line.

如果该指令成功执行,将创建一个中间映像,该映像将作为下一个要启动的容器的基础(执行以下命令的下一行)您的Dockerfile)

If the directive succeeds, that will create an intermediate image, which will be the base for the next container to be launch (to execute the next line of your Dockerfile)

如果上述指令失败,则可能使容器处于退出状态,这反过来将阻止从其创建的中间映像。

只需先清理所有容器,然后清理所有图像,然后重试。

If said directive fails, that can leave a container in an Exited state, which in turn will block the intermediate image it was created from.
Simply cleanup all the containers then all the images, and try again.

如果您尝试重复构建Dockerfile,最终会得到一系列中间映像和容器。

If you have tried to build repeatedly your Dockerfile, you end up with a collection of intermediate images and containers.

这就是为什么当我构建时(最终)成功,我总是在构建脚本

That is why, when my build (finally) succeeds, I always cleanup extra containers, images and (with docker 1.10+) volumes in my build script:

cmdb="docker build${proxy}${f} -t $1 $2"
# echo "cmdb='${cmdb}"
if eval ${cmdb}; then
    docker rm $(docker ps -qa --no-trunc --filter "status=exited" 2>/dev/null) 2>/dev/null
    docker rmi $(docker images --filter "dangling=true" -q --no-trunc 2>/dev/null) 2>/dev/null
    docker volume rm $(docker volume ls -qf dangling=true 2>/dev/null) 2>/dev/null
    exit 0
fi

这篇关于为什么当构建退出错误时,来自docker文件的docker构建映像会创建容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆