在我的 Dockerfile 中,我想定义以后可以在 Dockerfile 中使用的变量。
我知道 ENV
指令,但我不希望这些变量成为环境变量。
有没有办法在 Dockerfile 范围内声明变量?
最佳答案
您可以使用 ARG
- 见 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> .
关于variables - 如何在 Dockerfile 中定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33935807/