简体   繁体   English

mac 上没有显示环境变量

[英]Environment variables not showing up on mac

Environment variables set outside the shell, through ~/.bash_profile or ~/.bashrc do not appear to docker, or env, despite being accessible in the shell.在 shell 之外通过 ~/.bash_profile 或 ~/.bashrc 设置的环境变量不会出现在 docker 或 env 中,尽管可以在 Z2591C98B70119FE624898B1E424BE.5 中访问

Bash_profile contains the line TEST_ENV_VAR=123 , after restarting terminal, the variable can be accessed through $TEST_ENV_VAR , however docker and env cannot access this environment variable. Bash_profile 包含TEST_ENV_VAR=123行,重启终端后,可以通过$TEST_ENV_VAR访问该变量,但是 docker 和 env 无法访问此环境变量。

Henrys-MacBook-Pro:~ henry$ echo $TEST_ENV_VAR 
123          
Henrys-MacBook-Pro:~ henry$ docker run -it -e TEST_ENV_VAR ubuntu env | grep TEST_ENV_VAR
Henrys-MacBook-Pro:~ henry$ env | grep TEST_ENV_VAR

And yet the terminal can access it, and even pass it into docker:然而终端可以访问它,甚至将它传递给 docker:

Henrys-MacBook-Pro:~ henry$ docker run -it -e TEST_ENV_VAR=${TEST_ENV_VAR} ubuntu env | grep TEST_ENV_VAR
TEST_ENV_VAR=123

And the issue isn't an issue with environment variable in general, as variables set in the terminal work as expected:而且这个问题通常不是环境变量的问题,因为终端中设置的变量按预期工作:

Henrys-MacBook-Pro:~ henry$ export TEST_ENV_VAR=1234
Henrys-MacBook-Pro:~ henry$ docker run -it -e TEST_ENV_VAR ubuntu env | grep TEST_ENV_VAR
TEST_ENV_VAR=1234

I'm running macOS Mojave, 10.14.5, classic terminal, docker 19.03.4, the output of ls -al ~:我正在运行 macOS Mojave,10.14.5,经典终端,docker 19.03.4,ls -al ~ 的 output:

-rw-r--r--   1 henry  staff    455 Nov 12 11:50 .bash_profile

docker doesn't actually start a container. docker实际上并没有启动容器。 Instead, it sends a message to the Docker engine (running in a separate environment unrelated to the environment in which docker is executed) requesting that it start a container for you.相反,它会向 Docker 引擎(在与执行docker的环境无关的单独环境中运行)发送一条消息,请求为您启动一个容器。 As such, the new container won't inherit any of variables in your current shell environment.因此,新容器不会继承您当前 shell 环境中的任何变量。

With your TEST_ENV_VAR=${TEST_ENV_VAR} attempt, you are explicitly telling docker to create an environment variable named TEST_ENV_VAR , with the value produced by expanding TEST_ENV_VAR now , in the new container, rather than trying to inherit the variable from the appropriate environment.通过您的TEST_ENV_VAR=${TEST_ENV_VAR}尝试,您明确告诉docker创建一个名为TEST_ENV_VAR的环境变量,其值是通过在新容器中扩展TEST_ENV_VAR now产生的,而不是尝试从适当的环境继承变量。

Even ignoring that, you aren't actually creating an environment variable with TEST_ENV_VAR=123 ;即使忽略这一点,您实际上并没有使用TEST_ENV_VAR=123创建环境变量; you've only created an ordinary shell variable.您只创建了一个普通的 shell 变量。 For env to see it, you need to first export it.要让env看到它,您需要先导出它。

$ TEST_ENV_VAR=123
$ env | grep TEST_ENV_VAR
$ export TEST_ENV_VAR
$ env | grep TEST_ENV_VAR
TEST_ENV_VAR=123

The.bash_profile is for your user. .bash_profile 适用于您的用户。 Docker is running in its own environment (as opposed to running in a subshell spawned by your user). Docker 在其自己的环境中运行(而不是在用户生成的子shell 中运行)。 Export only sends information down into child shells.导出仅将信息向下发送到子 shell。 You need to make the variable available at a higher level.您需要使变量在更高级别可用。

/etc/environment 

Be careful in there.里面小心点。

Alternatively, you may look into asking Docker to make these changes itself.或者,您可以考虑要求 Docker 自行进行这些更改。

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

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