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?

7
  • Does this answer your question? How to set environment variables in Python? Commented Jun 20, 2023 at 17:00
  • Note: look at this answer more specifically: stackoverflow.com/a/43487762/6213883 Commented Jun 20, 2023 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 Commented Jun 20, 2023 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 stackoverflow.com/questions/43267413/… Commented Jun 20, 2023 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 Commented Jun 20, 2023 at 17:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.