Dockerfile之ARG指令详解及示例

Dockerfile之ARG指令详解及示例

2023年7月6日发(作者:)

Dockerfile之ARG指令详解及⽰例环境1. virtual box 6.12. centos 7.83. docker 19.03ARGARG [=]The

ARG instruction defines a variable that users can pass at build-time to the builder with the

docker build command usingthe

--build-arg = flag. If a user specifies a build argument that was not defined in the Dockerfile, the buildoutputs a 指令定义了⼀个变量,⽤户可以在构建时使⽤

docker build 命令使⽤--build-arg = 标志将其传递给构建器。如果⽤户指定了未在 Dockerfile 中定义的构建参数,则构建会输出警告。[Warning] One or more build-args [foo] were not consumed.A Dockerfile may include one or more

ARG 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-timevariable values are visible to any user of the image with the

docker history command.警告:不建议使⽤构建时变量来传递诸如 github 密钥,⽤户凭据等机密。构建时变量值对于使⽤

docker history 命令的镜像的任何⽤户都是可见的。默认值An

ARG instruction can optionally include a default value:ARG 指令可以选择包含默认值:FROM busyboxARG user1=someuserARG buildno=1# ...If an

ARG instruction has a default value and if there is no value passed at build-time, the builder uses the default.如果

ARG 指令具有默认值,并且在构建时未传递任何值,则构建器将使⽤默认值。范围An

ARG variable definition comes into effect from the line on which it is defined in the

Dockerfile not from the argument’suse 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 .The

USER at line 2 evaluates to

some_user as the

user variable is defined on the subsequent line 3. The

USER at line 4evaluates to

what_user as

user is defined and the

what_user value was passed on the command line. Prior to its definition byan

ARG 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 指令对其进⾏定义之前,对变量的任何使⽤都会导致⼀个空字符串。An

ARG 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 the

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

ARG 指令。FROM busyboxARG SETTINGSRUN ./run/setup $SETTINGSFROM busyboxARG SETTINGSRUN ./run/other $SETTINGS总结介绍了 Dockerfile 中 ARG 指令的说明,默认值和范围。

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688593035a153155.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信