I have a few environment variables that I need inside of my code.
And when I starts building a docker container, I wish to bake those environment variables into my docker container.
I understand that the dockerfile directive 'ENV' does exactly that, I wonder if there is a way to point to an file instead of writing it inside of the Dockerfile
AFAIK, there is no such way to inject environment variables using a file during the build step using Dockerfile. However, in most cases, people do end up using an entrypoint
script & injecting variables during the docker run
or docker-compose up
.
In case it's a necessity you might need to write a shell wrapper which will change the values in the Dockerfile
dynamically by taking a key-value pair text file as an input or make it something as below but the ENV file name need to be included in Dockerfile as suggested by @thaJeztah in link -
COPY my-env-vars /
RUN export $(cat my-env-vars | xargs)
It's an open issue -
https://github.com/moby/moby/issues/28617
At build time, ENV variables are not available. You need to use ARG variables. For that you have the "--build-arg" flag of "docker build". It doesn't take in a file thou.
ENV variables are used by a running container and can be injected as a file using the "--env-file" flag of "docker run".
Hopefully this is what you were looking for otherwise let me know more in detail.