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?

  • Automatically run a script when HBase master Docker container finishes initializing
  • Ubuntu Docker image broken out of the box?
  • Docker & Rails | bundler: command not found: rails
  • How can I debug my docker container with phpStorm
  • Docker client communicating with docker host
  • Terminal (bash?) / Docker: logic behind - and --
  • Importing a MySQL database with a Python script fails when the same command works on the command line, what gives?
  • Can't connect to 'docker' daemon on building kubernetes from source
  • Use docker-swarm with dokku
  • Is it possible to read messages passed over stdout from within a Docker container? (without `docker logs`)
  • Extending local Dockerfile
  • Graceful Shutdown of HDFS with Docker
  • 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 .

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.