I'm starting a docker container the following way:
docker run -e IP_AD=192.168.99.100 -p 80:80 flask_app
I'm simply trying to pass an IP Address to the flask application so that something can be loaded from my application. This resource will change from environment to environment, so this is the reason I would like to pass it as an environment variable.
Later, I would like to use this variable but from the context of the running flask application. How can I load IP_AD
from my flask application and use it as a python variable?
I've tried doing this:
import os
os.environ.get('IP_AD')
But it does not seem to be loading anything. What is the correct way to load IP_AD passed from docker run -e
答案 0 :(得分:0)
您可以尝试这样
import os
os.environ["IP_AD"]
答案 1 :(得分:0)
像这样创建文件 .env
model_path=./model/data_fs.csv
然后通过执行安装库.env
pip3 install python-dotenv==0.17.1
将此添加到您的代码中以加载您的所有 .env
变量
from dotenv import load_dotenv
load_dotenv()
所以你可以访问它
if os.getenv("model_path", None) is not None:
return os.getenv("model_path", None)
# otherwise raise an exception
raise Exception("no .env file found")