1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Dockerfile 之 ARG指令详解及示例

Dockerfile 之 ARG指令详解及示例

时间:2023-12-18 21:52:41

相关推荐

Dockerfile 之 ARG指令详解及示例

参考教程:/engine/reference/builder/

环境

virtual box 6.1centos 7.8docker 19.03

ARG

ARG <name>[=<default value>]

TheARGinstruction defines a variable that users can pass at build-time to the builder with thedocker buildcommand 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 moreARGinstructions. 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 thedocker historycommand.

警告:

不建议使用构建时变量来传递诸如 github 密钥,用户凭据等机密。构建时变量值对于使用docker history命令的镜像的任何用户都是可见的。

默认值

AnARGinstruction can optionally include a default value:

ARG指令可以选择包含默认值:

FROM busyboxARG user1=someuserARG buildno=1# ...

If anARGinstruction has a default value and if there is no value passed at build-time, the builder uses the default.

如果ARG指令具有默认值,并且在构建时未传递任何值,则构建器将使用默认值。

范围

AnARGvariable definition comes into effect from the line on which it is defined in theDockerfilenot 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 .

TheUSERat line 2 evaluates tosome_useras theuservariable is defined on the subsequent line 3. TheUSERat line 4 evaluates towhat_userasuseris defined and thewhat_uservalue was passed on the command line. Prior to its definition by anARGinstruction, any use of a variable results in an empty string.

第 2 行的USER评估为some_user,因为在随后的第 3 行中定义了USER变量。第 4 行的USER评估为what_user,因为定义了user,并且为what_user在命令行中传递。在通过ARG指令对其进行定义之前,对变量的任何使用都会导致一个空字符串。

AnARGinstruction 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 theARGinstruction.

ARG指令在定义它的构建阶段结束时超出范围。要在多个阶段使用变量,每个阶段都必须包含ARG指令。

FROM busyboxARG SETTINGSRUN ./run/setup $SETTINGSFROM busyboxARG SETTINGSRUN ./run/other $SETTINGS

总结

介绍了 Dockerfile 中 ARG 指令的说明,默认值和范围。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。