0

I'm trying to set an ENV equal to a python script output in my dockerfile. Due to layering, I'm unable to find a way to have them on the same layer. I've also been unable to find a way set the variable directly equal to the script, I can't seem to find how to run the script in the same line.

The python is a simple print, called test.py:

#!/usr/bin/env python

print("xd")

So far, I've tried a few versions of the below in my dockerfile to set the ENV to the script:

ENV testvar=test.py
ENV testvar=$(test.py)
ENV testvar='python test.py'

Is there anyway to set ENV testvar equal to the script in one step or a way to ensure the script and setting the variable are done on the same layer?

TriSarahTops
  • 63
  • 1
  • 5
  • 1
    Get your output string, then set the environment variable in a call to the function for setting the value in your script? – eagle33322 Feb 12 '19 at 19:14
  • 1
    If you are trying to call the script named test.py from something else, then I assume you could get the value in your script and eval that from wherever it is being used. Docker would let you RUN something at a different step in your dockerfile. – eagle33322 Feb 12 '19 at 19:18
  • I have tried that: `RUN python test.py ENV testvar=test.py` but because it starts a new layer each time, the ENV doesn't see the script result – TriSarahTops Feb 12 '19 at 19:22
  • 1
    Set it externally and write to the file, then run your dockerfile. – eagle33322 Feb 12 '19 at 19:25
  • 1
    https://stackoverflow.com/questions/34213837/dockerfile-output-of-run-instruction-into-a-variable – gold_cy Feb 12 '19 at 19:34
  • Didn't get the question properly. Are you trying to execute the python script inside Dockerfile and store the output in `testvar`? If so, then it is not possible, and not recommended. ENV variables are set before the docker engine runs Dockerfile, so you cannot go back and re-set it. – Rash Feb 14 '19 at 19:11

1 Answers1

0

I ended up being able to place this in my entrypoint and set it to a k8's secret (also set in the entrypoint) to get it to other pods.

Another option that was tried was placing the script output into a txt file, setting the variable to that file, then deleting it.

TriSarahTops
  • 63
  • 1
  • 5