Docker ENV for Python变量 [英] Docker ENV for Python variables
问题描述
是python&的新手docker,我创建了一个小烧瓶应用程序(test.py),该应用程序具有两个硬编码值:
Being new to python & docker, I created a small flask app (test.py) which has two hardcoded values:
username = "test"
password = "12345"
我能够创建Docker映像并从中运行容器以下Dockerfile:
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"]`
如何为用户名&创建ENV变量?密码并在运行容器时传递动态值?
How can I create a ENV variable for username & password and pass dynamic values while running containers?
推荐答案
在python代码中,您可以读取以下环境变量:
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> ...
这将在容器内设置env变量,稍后将由python读取脚本( test.py
)
This will set the env variable within the container and these will be later read by the python script (test.py
)
有关 os.environ 和 docker env
这篇关于Docker ENV for Python变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!