參考教程:https://docs.docker.com/engine/reference/builder/
環境
- virtual box 6.1
- centos 7.8
- docker 19.03
Environment replacement
Environment variables (declared with the ENV
statement) can also be used in certain instructions as variables to be interpreted by the Dockerfile
. Escapes are also handled for including variable-like syntax into a statement literally.
環境變量(用 ENV語句 聲明)也可以在某些指令中用作由 Dockerfile 解釋的變量。轉義也可以通過將類似變量的語法實際包含在語句中來進行處理。
Environment variables are notated in the Dockerfile
either with $variable_name
or ${variable_name}
. They are treated equivalently and the brace syntax is typically used to address issues with variable names with no whitespace, like ${foo}_bar
.
環境變量在 Dockerfile 中用 $variable_name
或 ${variable_name}
表示。它們被同等對待,並且大括號語法通常用於解決變量名稱沒有空格的問題,例如 ${foo}_bar
。
The ${variable_name}
syntax also supports a few of the standard bash
modifiers as specified below:
${variable_name}
語法還支持一些標准的 bash 修飾符,如下所示:
-
${variable:-word}
indicates that ifvariable
is set then the result will be that value. Ifvariable
is not set thenword
will be the result. -
${variable:+word}
indicates that ifvariable
is set thenword
will be the result, otherwise the result is the empty string. -
${variable:-word}
表示如果設置了variable
則結果將是該值。如果未設置variable
,那么將是word
。 -
${variable:+word}
表示如果設置了variable
則將以word
作為結果,否則結果為空字符串。
In all cases, word
can be any string, including additional environment variables.
在所有情況下,word
可以是任何字符串,包括其他環境變量。
Escaping is possible by adding a \
before the variable: \$foo
or \${foo}
, for example, will translate to $foo
and ${foo}
literals respectively.
可以通過在變量前添加一個\
來進行轉義:例如,\$foo
或 \${foo}
將分別轉換為 $foo
和 ${foo}
。
Example (parsed representation is displayed after the #
):
示例(解析的表示形式顯示在 #
之后):
FROM busybox
ENV FOO=/bar
WORKDIR ${FOO} # WORKDIR /bar
ADD . $FOO # ADD . /bar
COPY \$FOO /quux # COPY $FOO /quux
Environment variables are supported by the following list of instructions in the Dockerfile
:
Dockerfile 中的以下指令列表支持環境變量:
ADD
COPY
ENV
EXPOSE
FROM
LABEL
STOPSIGNAL
USER
VOLUME
WORKDIR
ONBUILD
(when combined with one of the supported instructions above)
Environment variable substitution will use the same value for each variable throughout the entire instruction. In other words, in this example:
在整個指令中,環境變量替換將對每個變量使用相同的值。換句話說,在此示例中:
ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$abc
will result in def
having a value of hello
, not bye
. However, ghi
will have a value of bye
because it is not part of the same instruction that set abc
to bye
.
將導致 def
的值為 hello
,而不是 bye
。但是,ghi
將具有 bye
的值,因為它不是將 abc
設置為 bye
的同一指令的一部分。
[root@master env]# docker build .
Sending build context to Docker daemon 2.048kB
Step 1/6 : FROM busybox
---> dc3bacd8b5ea
Step 2/6 : ENV abc=hello
---> Using cache
---> 2c674628ad0d
Step 3/6 : ENV abc=bye def=$abc
---> Using cache
---> 27401f4e57f7
Step 4/6 : ENV ghi=$abc
---> Using cache
---> ef6aa3e1c3ea
Step 5/6 : RUN echo $def
---> Running in 4cf63c33a97d
hello
Removing intermediate container 4cf63c33a97d
---> 72c1cae07a7c
Step 6/6 : RUN echo $ghi
---> Running in 3cba9330d595
bye
Removing intermediate container 3cba9330d595
---> a43f85633ec2
Successfully built a43f85633ec2
[root@master env]# cat Dockerfile
FROM busybox
ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$abc
RUN echo $def
RUN echo $ghi
[root@master env]#
總結
介紹了 Dockerfile 中的環境變量和 ENV 指令的使用。