从 Docker 容器中获取环境变量 [英] Get Environment Variable from Docker Container
问题描述
从 Docker 容器中获取环境变量的最简单方法是什么在 Dockerfile 中没有声明?
What's the simplest way to get an environment variable from a docker container that has not been declared in the Dockerfile?
例如,通过某些 docker exec container/bin/bash
会话设置的环境变量?
For instance, an environment variable that has been set through some docker exec container /bin/bash
session?
我可以做 docker exec container env |grep ENV_VAR
,但我更喜欢只返回值的东西.
I can do docker exec container env | grep ENV_VAR
, but I would prefer something that just returns the value.
我尝试使用 docker exec container echo "$ENV_VAR"
,但替换似乎发生在容器外部,所以我没有从容器中获取 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.
谢谢.
推荐答案
在容器内运行 echo "$ENV_VAR"
以便在容器中发生变量替换的正确方法是:
The proper way to run echo "$ENV_VAR"
inside the container so that the variable substitution happens in the container is:
docker exec <container_id> bash -c 'echo "$ENV_VAR"'
这篇关于从 Docker 容器中获取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!