Docker: How to set environment variables from file during build?

  build, docker, dockerfile, environment, python

I would like to set a list of environment variables as specified in an env.list file during the build process, i.e. have a respective command in the Dockerfile. Like this:

FROM python:3.9.4-slim-buster

COPY env.list env.list

# Here I need a corresponding command:
ENV env.list 

The file looks like this:

FOO=foo
BAR=bar

My book of already failed attempts / ruled out options:


On Linux, one can usually set environment variables from a file env.list by running:

source env.list
export $(cut -d= -f1 env.list)

However, executing those commands as RUN in the Dockerfile does not work because env variables defined using RUN export FOO=foo are not persisted across different stages of the image.


I do not want to explicitly set those variables in the Dockerfile using ENV FOO=foo because they contain login credentials. It’s also easier to automate/maintain the project if the variables are defined in one place.


I also don’t want to set those variables during docker run --env-file env.list because I need them for a development container which does not "run".

Source: Docker Questions

LEAVE A COMMENT