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?

  • Bind to docker socket on Windows
  • Rancher can pull images from private registry
  • how to provide environment variables to AWS ECS task definition?
  • Running Client program and API in same Docker Machine
  • Can't mount all files to docker container volume
  • Concurrent access to docker.sock on k8s
  • After installing Docker, docker-machine env default fails because ca.pem is not found
  • Enable Docker Remote API - raspberry pi / raspbian
  • How to determine bridge IP of docker swarm container
  • Error in docker container bash: vi: command not found
  • How to setup local Docker development with virtual host?
  • Docker for Windows and docker-maven-plugin - “SSLException: Unrecognized SSL message, plaintext connection” error
  • 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.