I am trying to have my python application running on a PEX file, inside a docker container.
I was wondering if PEX files support any kind of configuration in order to read environment variables that get provided to the docker container just to avoid packaging a PEX file per deplyoment environment having differrrent types of configuration.
I couldn't find any type of documentation on this on the PEX site.
Expectation was to be able to consume defined environment variables for the docker container from the python process running on the PEX file, but the PEX execution seems self-isolated.
Only documentation about runtime environment variables I could find is about PEX_* environment variables which doesn't seem to serve that purpose.
PEX programs receive all environment variables (except possibly for a few changes, e.g. in PYTHONPATH
), you can try this by writing import os; print(sorted(os.environ.items()))
to myprog.py
, and then running python myprog.py
, building myprog.pex
, running python myprog.pex
. An even more specific way to try:
$ wget -nv -O pex https://github.com/pex-tool/pex/releases/download/v2.2.1/pex
$ mkdir pp
$ echo "import os" >pp/myprog2.py
$ echo "def run(): print(os.environ['ANSWER'])" >>pp/myprog2.py
$ echo "if __name__ == '__main__': run()" >>pp/myprog2.py
$ env ANSWER=42 python pp/myprog2.py
42
$ echo "project.name = 'pp'" >pp/pyproject.toml
$ echo "project.version = '0.1'" >>pp/pyproject.toml
$ echo "project.scripts.myprog2 = 'myprog2:run'" >>pp/pyproject.toml
$ python pex ./pp -o pp/myprog2.pex -c myprog2
$ env ANSWER=42 python pp/myprog2.pex
42
Docker with the docker run
command doesn't pass host environment variables to the container. To pass some of them, use the -e
flag (e.g. docker run -e ANSWER=42 ...
), like this: https://stackoverflow.com/a/30494145/97248