In my Dockerfile, I would like to define variables that I can use later in the Dockerfile.
I am aware of the ENV
instruction, but I do no want these variables to be environment variables.
Is there a way to declare variables at Dockerfile scope?
In my Dockerfile, I would like to define variables that I can use later in the Dockerfile.
I am aware of the ENV
instruction, but I do no want these variables to be environment variables.
Is there a way to declare variables at Dockerfile scope?
To answer your question:
You can define a variable with:
Spaces around the equal character are not allowed.
and use it later with
You can use
ARG
- see https://docs.docker.com/engine/reference/builder/#argIf the variable is re-used within the same
RUN
instruction, one could simply set a shell variable. I really like how they approached this with the official Ruby Dockerfile.To my knowledge, only
ENV
allows that, as mentioned in "Environment replacement"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+, anddocker build --build-arg var=value
) andENV
.Using
ARG
alone means your variable is visible at build time, not at runtime.My Dockerfile usually has:
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.