skip to Main Content

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.

2

Answers


  1. 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.

    Now as you mention the container is not run locally, you can still set environment variables at runtime using mechanisms like Kubernetes ConfigMaps.

    Login or Signup to reply.
  2. 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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search