【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 指令的使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM