2

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

2 Answers 2

2

ENV directive does not allow to parse a file like env.list, as pointed out. But even if it did, the resulting environment variables would still be saved in the final image, passwords included.

The correct approach to my knowledge is to set the passwords at runtime with "docker run", when this image runs, or when the child image runs via "docker run".

If the credentials are required while the image is built, I would pass them via the ARG directive so that they can be reference as shell variables in the Dockerfile but not saved in the final image:

ARG VAR
FROM image

RUN echo ${VAR}
etc...

which can run as:

docker build --build-arg VAR=value ...
Sign up to request clarification or add additional context in comments.

Comments

0

If you use docker-compose you can pass a variables.env file

docker-compose.yml:

version: "3.7"
services:
 service_name:
   build: folder/.
   ports:
   - '5001:5000'
   env_file:
   - folder/variables.env

folder/Dockerfile

FROM python:3.9.4-slim-buster

folder/variables.env

FOO=foo
BAR=bar

For more info on compose: https://docs.docker.com/compose/

2 Comments

This will not work because env_file is not used when building an image.
env_file is used only in runtime not during build

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.