如何在Docker构建期间使构建arg为强制性? [英] How to make a build arg mandatory during Docker build?

查看:78
本文介绍了如何在Docker构建期间使构建arg为强制性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

docker build 期间,是否有任何方法可以强制使用build参数?如果缺少该参数,则预期的行为是使构建失败。

Is there any way to make a build argument mandatory during docker build? The expected behaviour would be for the build to fail if the argument is missing.

例如,对于以下Dockerfile:

For example, for the following Dockerfile:

FROM ubuntu

ARG MY_VARIABLE
ENV MY_VARIABLE $MY_VARIABLE

RUN ...

我希望使用 ARG MY_VARIABLE 构建失败 docker build -t my-tag。并在使用 docker build -t my-tag --build-arg MY_VARIABLE = my_value构建时通过。 code>。

I would like the build to fail at ARG MY_VARIABLE when built with docker build -t my-tag . and pass when built with docker build -t my-tag --build-arg MY_VARIABLE=my_value ..

有什么方法可以实现这种行为?就我而言,设置默认值并不能真正解决问题。

Is there any way to achieve that behaviour? Setting a default value doesn't really do the trick in my case.

(我正在 darwin / amd64 1.11.1 c $ c>。)

(I'm running Docker 1.11.1 on darwin/amd64.)

编辑
我能想到的一种方法是运行失败的命令当 MY_VARIABLE 为空时,例如:

FROM ubuntu

ARG MY_VARIABLE
RUN test -n "$MY_VARIABLE"
ENV MY_VARIABLE $MY_VARIABLE

RUN ...

,但这似乎并不是解决当前问题的非常惯用的解决方案。

but it doesn't seem to be a very idiomatic solution to the problem at hand.

推荐答案

我用 RUN测试-n< ARGvariablename> 在原始(编辑)帖子中提到了@konradstrack……负责强制将变量作为 docker build 命令的构建时间参数传递:

I tested with RUN test -n <ARGvariablename> what @konradstrack mentioned in the original (edit) post... that seems do the job of mandating the variable to be passed as the build time argument for the docker build command:

FROM ubuntu

ARG MY_VARIABLE
RUN test -n "$MY_VARIABLE"
ENV MY_VARIABLE $MY_VARIABLE

这篇关于如何在Docker构建期间使构建arg为强制性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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