如何在 Dockerfile 中定义变量? [英] How to define a variable in a Dockerfile?

查看:97
本文介绍了如何在 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 the docker 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆