0

I am trying to create a Dockerfile that copies a python script from my local to a docker container and this python script sets a few environment variables for me as part of the script running. This python script must also take three parameters as inputs to the script.

Below is what I have currently in the DockerFile: # Base image FROM apache/airflow:2.6.1

USER root

# Set working directory and copy python script
WORKDIR /usr/app/src 


COPY --chmod=755 python_script.py ./

ARG HOST=''
ARG USER=''
ARG PASSWORD=''

ENV HOST=${HOST}
ENV USER=${USER}
ENV PASSWORD=${PASSWORD}

CMD ["python", "python_script.py", "$HOST", "$USER", "$PASSWORD"]

My problem is that I am unable to call the environment variables that are being set in the python file via the below:

os.environ[env_name]=env_value

I am trying to accomplish this by running:

docker-compose build --build-arg HOST=<host> --build-arg USER=<user> --build-arg PASSWORD=<password>

docker-compose up -d

Would anyone be able to lend some insight into how I can go about accomplishing this?

  • Does this answer your question? [How to set environment variables in Python?](https://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python) – Itération 122442 Jun 20 '23 at 17:00
  • Note: look at this answer more specifically: https://stackoverflow.com/a/43487762/6213883 – Itération 122442 Jun 20 '23 at 17:00
  • Not really because the environment variables I am looking to set need to be accessed by Airflow which is running in Docker – Yusuf Cattaneo Jun 20 '23 at 17:03
  • Maybe instead of having your Python script call `os.environ` (which won't impact anything other than the Python process and its children), have it write a .env file, and add a `CMD` in your Docker file to apply the environment variables from that file, using something like https://stackoverflow.com/questions/43267413/how-to-set-environment-variables-from-env-file – slothrop Jun 20 '23 at 17:12
  • @slothrop tried that suggestion but I am getting a permission issue when I try to write out to a .env file, how can I get the necessary permissions to write out the value? Note i am using the root user in the DockerFile – Yusuf Cattaneo Jun 20 '23 at 17:40
  • hmm, sorry :( I don't know how to fix that if you are already running as root. – slothrop Jun 20 '23 at 17:51
  • where are you tryin to get the environment variable? into the script or into bash of the container that is running? – kiggyttass Jun 21 '23 at 22:06

0 Answers0