getgrav/docker-grav

Can't set GRAV_VERSION using environment variables

Hartmnt opened this issue · 0 comments

Problem

It is currently not possible to set the GRAV_VERSION variable without editing the Dockerfile by just using env variables.
The build process will always use GRAV_VERSION=latest no matter what option you specify. ENV type variables in the Dockerfile only matter in the container creation phase. [1]

Steps to reproduce
Try to build the image using
docker build --build-arg "GRAV_VERSION=1.6.31" -t grav:1.6.31 .

Expected
The resulting image will use Grav 1.6.31

Actual
The build process will use latest for the image anyway

Solution

The fix is trivial. By using ARG instead of ENV when declaring the variable, docker will allow the var to be changed at build time. [2]
If, for whatever reason, an ENV variable is also desired in the future, it can be added as well and initialized with the value of the ARG variable like this [3]:

# You can set VAR_A while building the image
# or leave it at the default
ARG VAR_A=5
# VAR_B gets the (overridden) value of VAR_A
ENV VAR_B $VAR_A

References:
[1] https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file
[2] https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg
[3] https://vsupalov.com/docker-arg-vs-env/