Detailed usage of the ENV directive in Dockerfile

  • 2021-06-28 14:28:20
  • OfStack

1. The ENV directive in Dockerfile defines mirrored environment variables.Examples are as follows:


RUN set -ex && apt-get update && apt-get install -y iputils-ping
ENV PATH /usr/local/bin:$PATH
ENV LANG C.UTF-8
ENV TERM xterm
ENV PYTHON_VERSION 3.5.3
ENV name1=ping name2=on_ip
CMD $name1 $name2

Description: While defining environment variables, you can also reference environment variables that have already been defined.

In the ENV directive, the following environment variables can be referenced directly:

HOME, User Home Directory HOSTNAME, host name of default container PATH, TERM, default xterm

2. Because of the mirrored hierarchical file system, the environment variables defined by ENV can only be applied at subsequent levels, as shown in the following examples:


ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$abc

Explain:

As a result of the above definition, def=hello, ghi=bye

3. After starting the container, you can view the environment variables through the env command in the container instance


env

Reference link:

https://docs.docker.com/engine/reference/builder/


Related articles: