A
A
Artem Matyashov2018-02-05 23:29:40
Python
Artem Matyashov, 2018-02-05 23:29:40

How to set an environment variable in Docker?

Good day!
In the secret.py file , a certain sequence of characters is generated and displayed on the screen

def gen_secret():
    return "some secret"
print(gen_secret())

There is also a set_secret.sh file that sets the SECRET environment variable
#!/bin/sh
export SECRET=$(python util/secret.py)
echo $SECRET

Later in the python application, I use the value of the SECRET variable . I use python:3.6.3-onbuild as a base image .
Is it possible to write something like source set_secret.sh in the Dockerfile so that when the container starts, the code in gen_secret () is executed and the environment variable is set?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2018-02-06
@fox_12

Option 1:
docker-compose.yml:

...   
         env_file: .env

or
...
       environment:
           - SECRET=value

.env:
2 option
or
docker run --env-file .env ...

S
santaatnas, 2018-02-06
@santaatnas


1) You can add environment:
SECRET : VALUE to your container directly in the docker-compose.yml file
2) Or write your bash script in the dockerfile.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question