0
votes

I have a docker-compose file that allows me to pass the environment variables as a file (.env file). As I have multiple ENV variables, Is there any option in Dockerfile like env_file in docker-compose for passing multiple environment variables during docker build?

This is the docker-compose.yml

services:
  web:
    image: "node"
    links:
        - "db"
    env_file: "env.app"
1
You can COPY your env.app in the Dockerfile and then source it with a RUN command. But there is not something like a built in command for it - Mihai

1 Answers

0
votes

AFAIK, there is no such way to inject environment variables using a file during the build step using Dockerfile. However, in most cases, people do end up using an entrypoint script & injecting variables during the docker run or docker-compose up.

In case it's a necessity you might need to write a shell wrapper which will change the values in the Dockerfile dynamically by taking a key-value pair text file as an input or make it something as below but the ENV file name need to be included in Dockerfile.

COPY my-env-vars /    
RUN export $(cat my-env-vars | xargs)

It's an open issue - https://github.com/moby/moby/issues/28617

PS - You need to be extra careful while using this approach because the secrets are baked into the image itself.