1. 程式人生 > 實用技巧 >【Docker】Dockerfile 之 ENV

【Docker】Dockerfile 之 ENV

參考教程:https://docs.docker.com/engine/reference/builder/

環境

  1. virtual box 6.1
  2. centos 7.8
  3. docker 19.03

ENV

ENV <key>=<value> ...

The ENV instruction sets the environment variable <key> to the value <value>. This value will be in the environment for all subsequent instructions in the build stage and can be

replaced inline in many as well. The value will be interpreted for other environment variables, so quote characters will be removed if they are not escaped. Like command line parsing, quotes and backslashes can be used to include spaces within values.

ENV 指令將環境變數 <key> 設定為值 <value>。此值將在構建階段的所有後續指令的環境中使用,也可以在很多情況下

內聯替換。該值將被其他環境變數解釋,因此如果不對引號字元進行轉義,則將其刪除。與命令列解析一樣,引號和反斜槓可用於在值中包含空格。

Example:

例如:

ENV MY_NAME="John Doe"
ENV MY_DOG=Rex\ The\ Dog
ENV MY_CAT=fluffy

The ENV instruction allows for multiple <key>=<value> ... variables to be set at one time, and the example below will yield the same net results in the final image:

ENV 指令允許一次設定多個 <key> = <value> ... 變數,下面的示例將在最終映象中產生相同的最終結果:

ENV MY_NAME="John Doe" MY_DOG=Rex\ The\ Dog \
    MY_CAT=fluffy

The environment variables set using ENV will persist when a container is run from the resulting image. You can view the values using docker inspect, and change them using docker run --env <key>=<value>.

當從結果映象執行容器時,使用 ENV 設定的環境變數將保留。您可以使用 docker inspect 檢視值,並使用 docker run --env <key> = <value> 更改它們。

Environment variable persistence can cause unexpected side effects. For example, setting ENV DEBIAN_FRONTEND=noninteractive changes the behavior of apt-get, and may confuse users of your image.

環境變數的永續性可能導致意外的副作用。例如,設定 ENV DEBIAN_FRONTEND=noninteractive 會更改 apt-get 的行為,並可能使映象使用者感到困惑。

If an environment variable is only needed during build, and not in the final image, consider setting a value for a single command instead:

如果僅在構建過程中需要環境變數,而在最終映像中則不需要,請考慮為單個命令設定一個值:

RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y ...

Or using ARG, which is not persisted in the final image:

或使用 ARG,它不會保留在最終映象中:

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y ...

Alternative syntax

替代語法

The ENV instruction also allows an alternative syntax ENV <key> <value>, omitting the =. For example:

ENV 指令還允許使用替代語法 ENV <key> <value>,而忽略 =。例如:

ENV MY_VAR my-value

This syntax does not allow for multiple environment-variables to be set in a single ENV instruction, and can be confusing. For example, the following sets a single environment variable (ONE) with value "TWO= THREE=world":

這種語法不允許在單個 ENV 指令中設定多個環境變數,這可能會造成混淆。例如,以下程式碼將單個環境變數(ONE)的值設定為 "TWO= THREE=world"

ENV ONE TWO= THREE=world

The alternative syntax is supported for backward compatibility, but discouraged for the reasons outlined above, and may be removed in a future release.

支援備用語法以實現向後相容,但出於上述原因,不建議使用該語法,並且在將來的版本中可能會刪除該語法。

示例

Dockerfile 檔案

FROM busybox
LABEL author=jiangbo
ENV name=jiangbo
CMD echo $name

構建結果

[root@master env]# docker build -t jiangbo:0.0.1 .
Sending build context to Docker daemon  3.584kB
Step 1/4 : FROM busybox
 ---> dc3bacd8b5ea
Step 2/4 : LABEL author=jiangbo
 ---> Running in e7a093e0fc49
Removing intermediate container e7a093e0fc49
 ---> 8129cae696ad
Step 3/4 : ENV name=jiangbo
 ---> Running in 8bdcffbe711e
Removing intermediate container 8bdcffbe711e
 ---> c8a646bfdab3
Step 4/4 : CMD echo $name
 ---> Running in 9cfc4f234843
Removing intermediate container 9cfc4f234843
 ---> b6f80c7fac3f
Successfully built b6f80c7fac3f
Successfully tagged jiangbo:0.0.1

檢視結果

[root@master env]# docker run jiangbo:0.0.1
jiangbo

總結

介紹了 Dockerfile 中 ENV 指令的使用。