如何在Docker构建期间使构建arg为强制性? [英] How to make a build arg mandatory during Docker build?
问题描述
在 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 $上运行Docker
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屋!