61

Being new to python & docker, I created a small flask app (test.py) which has two hardcoded values:

username = "test"
password = "12345"

I'm able to create a Docker image and run a container from the following Dockerfile:

FROM python:3.6

RUN mkdir /code  
WORKDIR /code  
ADD . /code/  
RUN pip install -r requirements.txt  

EXPOSE 5000  
CMD ["python", "/code/test.py"]`

How can I create a ENV variable for username & password and pass dynamic values while running containers?

4 Answers 4

94

Within your python code you can read env variables like:

import os
username = os.environ['MY_USER']
password = os.environ['MY_PASS']
print("Running with user: %s" % username)

Then when you run your container you can set these variables:

docker run -e MY_USER=test -e MY_PASS=12345 ... <image-name> ...

This will set the env variable within the container and these will be later read by the python script (test.py)

More info on os.environ and docker env

Sign up to request clarification or add additional context in comments.

2 Comments

Here is an article which shows how to set env variables at build time as opposed to setting them when running the container: blog.ruanbekker.com/blog/2018/04/07/…
Thank you, @urban! Just a point of attention for anyone not getting it to work: - Be aware that "-e your_env_var_name = env_var_value" must come BEFORE the image name in the RUN command
36

In your Python code you can do something like this:

 # USERNAME = os.getenv('NAME_OF_ENV_VARIABLE','default_value_if_no_env_var_is_set')
 USERNAME = os.getenv('USERNAME', 'test')

Then you can create a docker-compose.yml file to run your dockerfile with:

version: '2'
services:
  python-container:
    image: python-image:latest
    environment:
      - USERNAME=test
      - PASSWORD=12345

You will run the compose file with:

$ docker-compose up

All you need to remember is to build your dockerfile that you mentioned in your question with:

$ docker build -t python-image .

Let me know if that helps. I hope that answers your question.

2 Comments

Sensitive data written as plain text is OK in Dockerfile / Docker-compose.yml ?
It really depends on the context.
6

I split my docker-compose into docker-compose.yml (base), docker-compose.dev.yml, etc., then I had this issue.

I solved it by specifying the .env file explicitly in the base:

web:
    env_file:
      - .env

Not sure why, according to the docs it should just work if there's an .env file.

Comments

3

FROM python:3

MAINTAINER <[email protected]>

ENV username=test
    password=12345

RUN mkdir /dir/name

RUN cd /dir/name && pip3 install -r requirements.txt

WORKDIR /dir/name

ENTRYPOINT ["/usr/local/bin/python", "./test.py"]

1 Comment

every time you modify a docker, make sure to run the build command before running the image..

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.