从Docker容器获取环境变量

从Docker容器获取环境变量

本文介绍了从Docker容器获取环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ p

通过一些 docker exec container / bin / bash 会话设置的环境变量



我可以做 docker exec container env | grep ENV_VAR ,但我更喜欢只返回值的东西。



我尝试使用 docker exec container echo$ ENV_VAR,但替换似乎发生在容器外面,所以我没有从容器中获取env var,而是从我自己的电脑获取env var。



谢谢。

解决方案

运行在容器中回显$ ENV_VAR,以便在容器中发生变量替换:

  docker exec container bash -c'echo$ ENV_VAR'


What's the simplest way to get an environment variable from a docker container that has not been declared in the Dockerfile?

For instance, an environment variable that has been set through some docker exec container /bin/bash session?

I can do docker exec container env | grep ENV_VAR, but I would prefer something that just returns the value.

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.

Thanks.

解决方案

The proper way to run echo "$ENV_VAR" inside the container so that the variable substitution happens in the container is:

docker exec container bash -c 'echo "$ENV_VAR"'

这篇关于从Docker容器获取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:17