How to define a variable in a Dockerfile?

后端 未结 6 1182
情歌与酒
情歌与酒 2020-12-07 16:02

In my Dockerfile, I would like to define variables that I can use later in the Dockerfile.

I am aware of the

6条回答
  •  抹茶落季
    2020-12-07 16:41

    To my knowledge, only ENV allows that, as mentioned in "Environment replacement"

    Environment variables (declared with the ENV statement) can also be used in certain instructions as variables to be interpreted by the Dockerfile.

    They have to be environment variables in order to be redeclared in each new containers created for each line of the Dockerfile by docker build.

    In other words, those variables aren't interpreted directly in a Dockerfile, but in a container created for a Dockerfile line, hence the use of environment variable.


    This day, I use both ARG (docker 1.10+, and docker build --build-arg var=value) and ENV.
    Using ARG alone means your variable is visible at build time, not at runtime.

    My Dockerfile usually has:

    ARG var
    ENV var=${var}
    

    In your case, ARG is enough: I use it typically for setting http_proxy variable, that docker build needs for accessing internet at build time.

提交回复
热议问题