I have instructed Docker to inject an environment variable called "INJECT_THIS" using two different methodologies (env.list file via ‘docker run’, and Dockerfile). My Dockerfile’s RUN command invokes a Python file (run.py), but the environment variable is not seen by the Python code’s os.environ object:
environ({'OLDPWD': '/', 'PATH': '/command:/lsiopy/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'PWD': '/app', 'LC_CTYPE': 'C.UTF-8'})
I’ve created a reproducible example on Github, but will also paste file contents in this question.
Dockerfile
FROM linuxserver/blender
WORKDIR /app
ENV INJECT_THIS = "inject this"
COPY run.py ./
RUN apt-get update && apt-get -y install python3-pip
CMD ["python3", "-u", "run.py"]
run.py
import os
# Notice that the injected environment variable "INJECT_THIS" is not present.
print('os.environ', os.environ)
env.list
INJECT_THIS="inject this"
2
Answers
When you do
docker inspect <your image name>
you will see, there’sENTRYPOINT
specified. This entrypoint messes with environment variables.You can specify your
ENTRYPOINT
instead ofCMD
(note I removed the spaces inENV
):Then run the docker image:
Prints:
You can check the environment variables configured via