bash 从 Docker 容器获取环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34051747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Environment Variable from Docker Container
提问by Citronen
What's the simplest way to get an environment variable from a docker container that has not been declared in the Dockerfile?
从未在 Dockerfile 中声明的 docker容器获取环境变量的最简单方法是什么?
For instance, an environment variable that has been set through some docker exec container /bin/bash
session?
例如,通过某个docker exec container /bin/bash
会话设置的环境变量?
I can do docker exec container env | grep ENV_VAR
, but I would prefer something that just returns the value.
我可以docker exec container env | grep ENV_VAR
,但我更喜欢只返回值的东西。
I've tried using docker exec container echo "$ENV_VAR"
, but the substitution seems to happen outside of the container, so I don't get the env var from the container, but rather the env var from my own computer.
我试过使用docker exec container echo "$ENV_VAR"
,但替换似乎发生在容器之外,所以我没有从容器中获取 env var,而是从我自己的计算机中获取 env var。
Thanks.
谢谢。
回答by jwodder
The proper way to run echo "$ENV_VAR"
inside the container so that the variable substitution happens in the container is:
echo "$ENV_VAR"
在容器内运行以便在容器中发生变量替换的正确方法是:
docker exec <container_id> bash -c 'echo "$ENV_VAR"'
回答by aisbaa
To view all env variables:
查看所有环境变量:
docker exec container env
To get one:
得到一个:
docker exec container env | grep VARIABLE | cut -d'=' -f2
回答by gentooboontoo
You can use printenv VARIABLE
instead of /bin/bash -c 'echo $VARIABLE
. It's much simpler and it does not perform substitution:
您可以使用printenv VARIABLE
代替/bin/bash -c 'echo $VARIABLE
. 它更简单,并且不执行替换:
docker exec container printenv VARIABLE
回答by Sergiy Sokolenko
The downside of using docker exec
is that it requires a runningcontainer, so docker inspect -f
might be handy if you're unsure a container is running.
使用的缺点docker exec
是它需要一个正在运行的容器,因此docker inspect -f
如果您不确定容器是否正在运行,可能会很方便。
Example #1.Output a list of space-separated environment variables in the specified container:
示例#1。输出指定容器中以空格分隔的环境变量列表:
docker inspect -f \
'{{range $index, $value := .Config.Env}}{{$value}} {{end}}' container_name
the output will look like this:
输出将如下所示:
ENV_VAR1=value1 ENV_VAR2=value2 ENV_VAR3=value3
Example #2.Output each env var on new line and grep
the needed items, for example, the mysql container's settings could be retrieved like this:
示例#2。在新行上输出每个 env var 和grep
所需的项目,例如,可以像这样检索 mysql 容器的设置:
docker inspect -f \
'{{range $index, $value := .Config.Env}}{{println $value}}{{end}}' \
container_name | grep MYSQL_
will output:
将输出:
MYSQL_PASSWORD=secret
MYSQL_ROOT_PASSWORD=supersecret
MYSQL_USER=demo
MYSQL_DATABASE=demodb
MYSQL_MAJOR=5.5
MYSQL_VERSION=5.5.52
Example #3.Let's modify the example above to get a bash friendlyoutput which can be directly used in your scripts:
示例#3。让我们修改上面的例子以获得一个bash 友好的输出,它可以直接在你的脚本中使用:
docker inspect -f \
'{{range $index, $value := .Config.Env}}export {{$value}}{{println}}{{end}}' \
container_name | grep MYSQL
will output:
将输出:
export MYSQL_PASSWORD=secret
export MYSQL_ROOT_PASSWORD=supersecret
export MYSQL_USER=demo
export MYSQL_DATABASE=demodb
export MYSQL_MAJOR=5.5
export MYSQL_VERSION=5.5.52
If you want to dive deeper, then go to Go's text/templatepackage documentation with all the details of the format.
如果您想更深入地研究,请访问 Go 的文本/模板包文档,其中包含格式的所有详细信息。
回答by sarink
None of the above answers show you how to extract a variable from a non-running container (if you use the echo
approach with run
, you won't get any output).
上述答案都没有向您展示如何从非运行容器中提取变量(如果您使用echo
方法run
,您将不会得到任何输出)。
Simply run
with printenv
, like so:
简单地run
使用printenv
,就像这样:
docker run --rm <container> printenv <MY_VAR>
(Note that docker-compose
instead of docker
works too)
(请注意,docker-compose
代替也docker
有效)
回答by ddieppa
If by any chance you use VSCodeand has installed the docker extension, just right+clickon the docker you want to check (within the docker extension), clickon Inspect, and there search for env, you will find all your env variablesvalues
万一您使用VSCode并安装了泊坞窗延伸,只是right+click您要检查(该范围内的泊坞窗泊坞窗扩展),click对检查,并有搜索ENV,你会发现所有的ENV变量值
回答by Sathish
We can modify entrypoint
of a non-running container with the docker run
command.
我们可以entrypoint
使用docker run
命令修改非运行容器。
Example show PATH environment variable:
示例显示 PATH 环境变量:
using
bash
andecho
: This answerclaims thatecho
will not produce any output, which is incorrect.docker run --rm --entrypoint bash <container> -c 'echo "$PATH"'
using
printenv
docker run --rm --entrypoint printenv <container> PATH
using
bash
andecho
:这个答案声称echo
不会产生任何输出,这是不正确的。docker run --rm --entrypoint bash <container> -c 'echo "$PATH"'
使用
printenv
docker run --rm --entrypoint printenv <container> PATH
回答by Citronen
@aisbaa's answer works if you don't care when the environment variable was declared. If you want the environment variable, even if it has been declared inside of an exec /bin/bash
session, use something like:
如果您不关心环境变量的声明时间,@aisbaa 的答案会起作用。如果您想要环境变量,即使它已在exec /bin/bash
会话内声明,也可以使用以下内容:
IFS="=" read -a out <<< $(docker exec container /bin/bash -c "env | grep ENV_VAR" 2>&1)
It's not very pretty, but it gets the job done.
它不是很漂亮,但它完成了工作。
To then get the value, use:
要获取该值,请使用:
echo ${out[1]}
回答by MatteoOreficeIT
This command inspects docker stack processes' environment in the host :
此命令检查主机中 docker 堆栈进程的环境:
pidof dockerd containerd containerd-shim | tr ' ' '\n' \
| xargs -L1 -I{} -- sudo xargs -a '/proc/{}/environ' -L1 -0