Docker environment variables from file

I am trying to use a single Docker file for my production and development. The only difference between production and development is the environment variables that I set. Therefore, I would like to somehow import environment variables from a file. Before using Docker, I just do the following

. ./setvars ./main.py 

However, if the change is ./main.py with the equivalent of Docker

 . ./setvars docker run .... ./main.py 

then the variables will be on the host and not accessible from the Docker instance. Of course, a quick and dirty hack would be to create a file with

 #!/bin/bash . ./setvars ./main.py 

and run this on the instance. This, however, would be very unpleasant, since I had many scripts that I would like to run (with the same environment variables), and then you will have to create an additional script for each of them.

Is there any other solution for getting environment variables inside dockers without using another Docker file and the method described above?

+6
source share
1 answer

Your best --env-file is to use either the -e flag or --env-file of the docker run .

  • The -e flag allows you to specify key / value pairs of the env variable,
    eg:

     docker run -e ENVIRONMENT=PROD 

    You can use the -e flag multiple times to specify multiple env
    variables. For example, the docker registry itself is configured using -e flags , see https://docs.docker.com/registry/deploying/#running-a-domain-registry

  • The -env file allows you to specify a file. But each line of the file must be of type VAR = VAL

Full documentation:

https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables-e-env-env-file

+9
source

All Articles