参考教程:/engine/reference/builder/
环境
virtual box 6.1centos 7.8docker 19.03ARG
ARG <name>[=<default value>]
TheARG
instruction defines a variable that users can pass at build-time to the builder with thedocker build
command using the--build-arg <varname>=<value>
flag. If a user specifies a build argument that was not defined in the Dockerfile, the build outputs a warning.
ARG
指令定义了一个变量,用户可以在构建时使用docker build
命令使用--build-arg <varname>=<value>
标志将其传递给构建器。如果用户指定了未在 Dockerfile 中定义的构建参数,则构建会输出警告。
[Warning] One or more build-args [foo] were not consumed.
A Dockerfile may include one or moreARG
instructions. For example, the following is a valid Dockerfile:
Dockerfile 可能包含一个或多个ARG
指令。例如,以下是有效的 Dockerfile:
FROM busyboxARG user1ARG buildno# ...
Warning:
It is not recommended to use build-time variables for passing secrets like github keys, user credentials etc. Build-time variable values are visible to any user of the image with the
docker history
command.
警告:
不建议使用构建时变量来传递诸如 github 密钥,用户凭据等机密。构建时变量值对于使用
docker history
命令的镜像的任何用户都是可见的。
默认值
AnARG
instruction can optionally include a default value:
ARG
指令可以选择包含默认值:
FROM busyboxARG user1=someuserARG buildno=1# ...
If anARG
instruction has a default value and if there is no value passed at build-time, the builder uses the default.
如果ARG
指令具有默认值,并且在构建时未传递任何值,则构建器将使用默认值。
范围
AnARG
variable definition comes into effect from the line on which it is defined in theDockerfile
not from the argument’s use on the command-line or elsewhere. For example, consider this Dockerfile:
ARG
变量从 Dockerfile 中定义的行开始生效,而不是从命令行或其他地方的自变量使用开始。例如,考虑以下 Dockerfile:
FROM busyboxUSER ${user:-some_user}ARG userUSER $user# ...
A user builds this file by calling:
$ docker build --build-arg user=what_user .
TheUSER
at line 2 evaluates tosome_user
as theuser
variable is defined on the subsequent line 3. TheUSER
at line 4 evaluates towhat_user
asuser
is defined and thewhat_user
value was passed on the command line. Prior to its definition by anARG
instruction, any use of a variable results in an empty string.
第 2 行的USER
评估为some_user
,因为在随后的第 3 行中定义了USER
变量。第 4 行的USER
评估为what_user
,因为定义了user
,并且为what_user
在命令行中传递。在通过ARG
指令对其进行定义之前,对变量的任何使用都会导致一个空字符串。
AnARG
instruction goes out of scope at the end of the build stage where it was defined. To use an arg in multiple stages, each stage must include theARG
instruction.
ARG
指令在定义它的构建阶段结束时超出范围。要在多个阶段使用变量,每个阶段都必须包含ARG
指令。
FROM busyboxARG SETTINGSRUN ./run/setup $SETTINGSFROM busyboxARG SETTINGSRUN ./run/other $SETTINGS
总结
介绍了 Dockerfile 中 ARG 指令的说明,默认值和范围。