Is there anyway to have a DockerBuild file use an environment variables file?
I have a Docker container that will have a LARGE (about 100) number of customization settings. I would prefer to have more flexibility than coding the defaults all into the DockerFile. I notice that the docker run
command supports this option:
--env-file value Read in a file of environment variables (default [])
Is there any way to have a similar file-driven mechanism used by docker build
? Or is there a better way entirely?
2 Solutions collect form web for “Is there anyway to have a DockerBuild file use an environment variables file?”
Environment files are not supported for builds.
This has been discussed at large and very likely won’t be added.
Introducing env in the build command creates host dependent builds.
The ARG
command and --build-args
are available during builds but do not support a file like you require.
As user2105103 suggests, you can COPY the env file in your build steps and then source it during a RUN.
COPY .env /.env
RUN set -uex; \
. /.env; \
echo $MY_ENV_VAR
You can write a bash
script that would dynamically build your custom Dockerfiles
.