简体   繁体   English

在 shell 中设置运行时在容器中派生的环境变量

[英]Setting environment variable derived in container at runtime within a shell

I have custom entrypoint where a environment variable is exported.我有导出环境变量的自定义入口点。 The value of the environment variable is constructed using two other variables provided at runtime.环境变量的值是使用运行时提供的另外两个变量构造的。

Snippet from Dockerfile CMD ["bash", "/opt/softwareag/laas-api-server/entrypoint.sh"] .来自 Dockerfile CMD ["bash", "/opt/softwareag/laas-api-server/entrypoint.sh"]的片段。 Snippet from entrypoint.sh来自 entrypoint.sh 的片段

export URL="$SCHEME://$HOST:$PORT"
echo "URL:$URL"

Command docker run -e HOST="localhost" -e PORT="443" mycentos prints URL:localhost:443 as expected but the same variable appears to have lost the value when the following command is executed.命令docker run -e HOST="localhost" -e PORT="443" mycentos按预期打印 URL:localhost:443 但相同的变量在执行以下命令时似乎丢失了值。

docker exec -ti <that-running-container-from-myimage> bash 
container-prompt> echo $URL
<empty-line>

Why would the exported variable appear to have lost the value of URL?为什么导出的变量似乎丢失了 URL 的值? What is getting lost here?这里丢失了什么?

The environment variable will not persist across all bash session.环境变量不会在所有 bash session 中持续存在。 when the container run it will only available in that entrypoint session but later it will not available if it set using export .当容器运行时,它将仅在该入口点 session 中可用,但稍后如果使用export设置,它将不可用。

docker ENV vs RUN export docker ENV 与 RUN 导出

If you want to use across all session you should set them in Dockerfile.如果你想在所有 session 中使用,你应该在 Dockerfile 中设置它们。

ENV SCHEME=http
ENV HOST=example.com
ENV PORT=3000

And in the application side, you can use them together.also it will be available for all session.而在应用方面,您可以一起使用它们。它也将适用于所有 session。

curl "${SCHEME}://${HOST}:${PORT}
#
Step 8/9 : RUN echo "${SCHEME}://${HOST}:${PORT}"
 ---> Running in afab41115019
http://example.com:3000

Now if we look into the way you are using, it will not work because现在,如果我们调查您使用的方式,它不会起作用,因为

export URL="$SCHEME://$HOST:$PORT"
# only in this session
echo "URL:$URL"
# will be available for node process too but for this session only
node app.js

For example look into this Dockerfile例如查看这个 Dockerfile

FROM node:alpine
RUN echo $'#!/bin/sh \n\ 
     export URL=example.com  \n\
     echo "${URL}" \n\
     node -e \'console.log("ENV URL value inside nodejs", process.env.URL)\' \n\
     exec "$@" \n\
     ' >> /bin/entrypoint.sh
RUN chmod +x /bin/entrypoint.sh
entrypoint ["entrypoint.sh"]

So you when you Run docker container for the first time you will able to see the expected response.因此,当您第一次运行 docker 容器时,您将能够看到预期的响应。

docker run -it --rm myapp
example.com
ENV URL value inside nodejs example.com

Now we want to check for later session.现在我们要检查以后的 session。

docker run -it --rm abc  tail -f /dev/null
example.com
ENV URL value inside nodejs example.com

so the container is up during this time, we can verify for another session因此容器在此期间已启动,我们可以验证另一个 session

docker exec -it myapp sh -c "node -e 'console.log(\"ENV URL value inside nodejs\", process.env.URL)'"
ENV URL value inside nodejs undefined

As we can same script but different behaviour because of docker, so the variable is only available in that session, you can write them to file if you are interested in later use.由于 docker,我们可以使用相同的脚本但行为不同,因此该变量仅在 session 中可用,如果您有兴趣以后使用,可以将它们写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM