I am trying to set an environment variable for a container that is readable from my python script.
The python script is run via a containerized cron job. When running the script directly in the container, without the cronjob, I am able to read my environment variables.
My Dockerfile is
FROM python:3.7-slim
# install crontab
RUN apt-get update && apt-get -y install -qq cron
# install crontab
ENV TESTING=1
ENV CONTAINER_HOME=/opt/
WORKDIR $CONTAINER_HOME
ADD runner.py .
COPY test-cron /etc/cron.d/test-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/test-cron
RUN crontab /etc/cron.d/test-cron
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log
The cron job that runs the script
# every 1 minutes
* * * * * cd /opt/ && /usr/local/bin/python runner.py >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.
Lastly the script that is run
import os
print(os.environ)
I have also created a minimal example here: https://github.com/AndrewRPorter/python-docker-env.