Home > Software design >  Set environment variables from a file in Dockerfile
Set environment variables from a file in Dockerfile

Time:07-30

I know that I can use ENV to set environment variables one by one in my Dockerfile.

My question is that, is there a way (other than perhaps adding some script to bashrc in the image) to set the environment variables inside the Dockerfile from a .env file?

Note: In case this is not clear enough, I'm not talking about passing variables in docker run, I want the container to run with environment variables already set.

CodePudding user response:

Rather than specifying single environment variables on the docker run command one by one, you can use --env-file to specify lots of them via a file.

If you want to push all currently set variables into the container at runtime, use a combination of

printenv > env.txt
docker run --env-file env.txt ...

If you need the same at container build time, maybe have a command in Dockerfile to copy env.txt into the container and source it from within.

CodePudding user response:

You can do few other things:

  • pass to the cli the param "-e key=val" to override your environment variables
  • mount a volume in which locate a toto_config.toml file for example and parse the values with your application
  • if your container has this directory, put your env variables file in /etc/profile.d/
  • otherwise as it has already been said docker run --env-file envfile
  • Related