[英]How to access environmental variables from docker in python?
I have a Docker file which list environmental variable as follows:我有一个 Docker 文件,其中列出了如下环境变量:
ENV WORKGROUP "my-workgroup"
In python file I am accessing it as follows:在 python 文件中,我按如下方式访问它:
os.environ['WORKGROUP']
When I build the docker it fails with the following error:当我构建 docker 时,它失败并出现以下错误:
KeyError: 'WORK_GROUP'
The reason is some environmental variables have some default values which I need to set in docker.原因是一些环境变量有一些默认值,我需要在 docker 中设置它们。 I can override these inside docker run command when needed.
我可以在需要时在 docker 运行命令中覆盖这些。 Any idea how can I achieve such a scenario.
知道如何实现这样的场景。 Thanks.
谢谢。
I am usually doing like this in Dockerfile我通常在 Dockerfile 中这样做
ENV ENV_VAR=some_default_value
to start that kind of image and override env in image you can do this要启动那种图像并覆盖图像中的 env 你可以这样做
docker run --env ENV_VAR=value_to_override
ENV_VAR will have some_default_value
or value_to_override
when image is started. ENV_VAR 将在图像启动时具有
some_default_value
或value_to_override
。 (not during image build process). (不是在图像构建过程中)。 and
os.environ['ENV_VAR']
sees that value just fine.并且
os.environ['ENV_VAR']
认为该值很好。
you may try docker build --no_cache
so you are sure that image is rebuild completely.您可以尝试
docker build --no_cache
以确保映像已完全重建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.