如何在 Dockerfile 中定义变量? [英] How to define a variable in a Dockerfile?
问题描述
在我的 Dockerfile 中,我想定义变量,我可以稍后在 Dockerfile 中使用.
我知道 ENV
指令,但我不希望这些变量成为环境变量.
有没有办法在 Dockerfile 范围内声明变量?
你可以使用 ARG
- 见 https://docs.docker.com/engine/reference/builder/#arg
ARG
指令定义了一个用户可以传递的变量使用 docker build
命令为构建器构建时间--build-arg <varname>=<value>
标志.如果用户指定构建Dockerfile 中未定义的参数,构建输出错误.
<块引用>
在构建期间可以与 COPY 一起使用(例如,复制特定文件夹等标签特定内容)例如:
ARG MODEL_TO_COPY复制应用程序 ./application复制 $MODEL_TO_COPY ./application/$MODEL_TO_COPY
<块引用>
在构建容器时:
docker build --build-arg MODEL_TO_COPY=model_name -t :.
In my Dockerfile, I would like to define variables that I can use later in the Dockerfile.
I am aware of the ENV
instruction, but I do no want these variables to be environment variables.
Is there a way to declare variables at Dockerfile scope?
You can use ARG
- see https://docs.docker.com/engine/reference/builder/#arg
The
ARG
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 an error.
Can be useful with COPY during build time (e.g. copying tag specific content like specific folders) For example:
ARG MODEL_TO_COPY
COPY application ./application
COPY $MODEL_TO_COPY ./application/$MODEL_TO_COPY
While building the container:
docker build --build-arg MODEL_TO_COPY=model_name -t <container>:<model_name specific tag> .
这篇关于如何在 Dockerfile 中定义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!