I’ve a problem with my docker container. I want to set a ENV variable through the `docker run` command using the `–env ENV=VALUE` parameter. If I lookup the ENV in my docker entrypoint script using the bash command `env`, my ENV variables are shown like i’ve set them in the `docker run`. But when I do `os.environ` or `os.system(“env”)` inside of the container, the ENV variables are not showing. The ENV I’ve set in my Dockerfile is listed via the bash command and in python.
I’m using the python:3.12-alpine3.20
docker image. My Dockerfile:
# This is the build stage for getting all python libs. Mainly it's for reducing the final image size because for building and installing the pips, many tools are needed which are just bloat to the final image.
FROM python:3.12-alpine3.20 AS pip_build_stage
COPY ./python/requierements.txt /
RUN apk add
make
git
glib-dev
gcc
build-base
freetype-dev
libpng-dev
openblas-dev
libxml2-dev
libxslt-dev
# BluePy needs to be build here... idk why but pip install fails on alpine somehow
RUN git clone https://github.com/IanHarvey/bluepy.git &&
cd bluepy &&
python3.12 setup.py build &&
python3.12 setup.py install &&
cd /
# Normal pip install for pyyaml failed on RPI4, so I build it here
RUN git clone https://github.com/yaml/pyyaml.git &&
cd pyyaml &&
python3.12 setup.py build &&
python3.12 setup.py install &&
cd /
RUN pip3.12 install -r requierements.txt
# This is the stage for the final image
FROM python:3.12-alpine3.20
ARG ARCH=""
WORKDIR /src
COPY ./python/src/ .
COPY ./python/docker_entrypoint.sh /
RUN mkdir data
RUN touch DOCKER
VOLUME /src/data
RUN apk add --no-cache sudo bluez tzdata
ENV TZ=Europe/Berlin
# Copy pips from the pip build stage
COPY --from=pip_build_stage /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=pip_build_stage /usr/local/bin /usr/local/bin
ENTRYPOINT sh /docker_entrypoint.sh
I start the container with:
docker run --cap-add=SYS_ADMIN --cap-add=NET_ADMIN --net=host --name=MY_CONTAINER --restart=on-failure --volume=/var/run/dbus/:/var/run/dbus/ --volume=data:/src/data --interactive --tty --attach=stdout --attach=stdin --env LOOP=10 IMAGE:TAG
ENV listing using env
bash command in my docker_entrypoint.sh
inside of the container:
HOSTNAME=H-Desktop
PYTHON_PIP_VERSION=24.0
SHLVL=2
HOME=/root
LOOP=10
GPG_KEY=7169605F62C751356D054A26A821E680E5FA6305
PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/dbf0c85f76fb6e1ab42aa672ffca6f0a675d9ee4/public/get-pip.py
TERM=xterm
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG=C.UTF-8
PYTHON_VERSION=3.12.3
PWD=/src
PYTHON_GET_PIP_SHA256=dfe9fd5c28dc98b5ac17979a953ea550cec37ae1b47a5116007395bfacff2ab9
TZ=Europe/Berlin
ENV listing using os.system("env")
in my main.py inside of the container:
SUDO_GID=0
MAIL=/var/mail/root
USER=root
HOSTNAME=H-Desktop
SHLVL=1
HOME=/root
SUDO_UID=0
LOGNAME=root
TERM=xterm
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG=C.UTF-8
SUDO_COMMAND=/usr/local/bin/python3.12 main.py
SHELL=/bin/sh
SUDO_USER=root
PWD=/src
TZ=Europe/Berlin
ENV listing using print(os.environ)
in my main.py inside of the container:
{
"HOSTNAME": "H-Desktop",
"TERM": "xterm",
"PATH": "/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"LANG": "C.UTF-8",
"TZ": "Europe/Berlin",
"MAIL": "/var/mail/root",
"LOGNAME": "root",
"USER": "root",
"HOME": "/root",
"SHELL": "/bin/sh",
"SUDO_COMMAND": "/usr/local/bin/python3.12 main.py",
"SUDO_USER": "root",
"SUDO_UID": "0",
"SUDO_GID": "0"
}
The TZ
variable for the timezone, which was set in my Dockerfile is (like expected) showing up inside the container for python but the LOOP
variable isn’t there… somehow. I don’t know why that’s a bit of strange behaviour since it’s showing up when I do the env
via bash directly.
How can I access the ENV variables using python inside of my docker container?