mad_
mad_

Reputation: 8273

How to run Python command in Docker and capture output and set it to environment variable

I'm trying to run a simple command for which I do not want to create a new python file and use it in docker run command rather I just want to run the command and get the output of the python command to set the environment variable.

ENV SECRET_KEY = "python code generating the secret key for the app"

Upvotes: 0

Views: 619

Answers (1)

cglacet
cglacet

Reputation: 10961

Simply use the VAR="x" cmd form:

FROM python:3.9-rc

RUN SECRET_KEY=$(python -c 'print("Here is a secret")') python -c 'import os; print(os.environ["SECRET_KEY"])'

Which should work as expected (2nd python command will have access to SECRET_KEY):

$ docker build -t secret_test . && docker run secret_test

Sending build context to Docker daemon  891.4kB
Step 1/2 : FROM python:3.9-rc
 ---> 0e09b0645b9e
Step 2/2 : RUN SECRET_KEY=$(python -c 'print("Here is a secret!")') python -c 'import os; print(os.environ["SECRET_KEY"])'
 ---> Running in 836e869b7e10
Here is a secret
Removing intermediate container 836e869b7e10
 ---> e95b48fc1fde
Successfully built e95b48fc1fde
Successfully tagged secret_test:latest

Upvotes: 1

Related Questions