Docker如何运行解释动态生成的--env参数 [英] How does docker run interpret dynamically generated --env arguments

查看:1015
本文介绍了Docker如何运行解释动态生成的--env参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向docker run提供动态生成的--env VAR1 --env VAR2 --env-file env.list环境变量列表.

I am trying to provide a dynamically generated list of --env VAR1 --env VAR2 --env-file env.list environment variables to docker run.

不幸的是,它无法正常工作.

Unfortunately it is not working.

  • 对于--env映射变量,这些变量在容器中不可见.
  • 对于--env-file提供的文件,docker抱怨找不到文件:docker: open "env.list": no such file or directory.
  • for --env mapped variables, the variables are not visible in the container.
  • for --env-file provided file, docker complains that it cannot find the file: docker: open "env.list": no such file or directory.

详细信息

运行:

# env_params contains either --env or --env-file arguments
MY_VAR=123
env_params='--env "MY_VAR"'
echo ${env_params} 

docker run -it --rm \
    ${env_params} \
    my_docker_image env | grep MY_VAR

将不会输出任何内容. MY_VAR在容器内部不可见.但是:

will not output anything. MY_VAR is not visible inside the container. But:

MY_VAR=123
docker run -it --rm \
    --env "MY_VAR" \
    my_docker_image env | grep MY_VAR

将起作用,并且将打印123.

will work and 123 will be printed.

以类似的方式,--env-file在通过env_params提供时将不起作用,但在直接提供给docker run命令时将起作用.

In a similar way --env-file will not work when provided through env_params but will work when provided directly to the docker run command.

我在做什么错了?

推荐答案

这里有两个问题.

首先,在运行时,在您的shell中:

First, When you run, in your shell:

MY_VAR=123

您没有 not 设置环境变量.您已经设置了本地shell变量.当您使用--env MY_VAR时,您告诉Docker您想使 environment 变量MY_VAR在容器内可用,并且由于它不存在,您什么也没得到:

You have not set an environment variable. You have set a local shell variable. When you use --env MY_VAR, you are telling Docker that you want to make the environment variable MY_VAR available inside the container, and since it doesn't exist you get nothing:

$ MY_VAR=123
$ docker run -it --rm -e MYVAR alpine env | grep MY_VAR
<crickets>

如果您首先将其导出到环境:

If you first export that to the environment:

$ export MY_VAR=123
$ docker run -it --rm -e MYVAR alpine env | grep MY_VAR
MY_VAR=123

然后它将按您期望的那样工作.或者,您可以使用--env选项的VARNAME=VARVALUE形式:

Then it will work as you expect. Alternately, you can use the VARNAME=VARVALUE form of the --env option:

docker run -e "MY_VAR=${MY_VAR}" ...

第二个问题与外壳变量插值的工作方式有关.如果您有:

The second issue has to do with how shell variable interpolation works. If you have:

env_params='--env "MY_VAR"'   
docker run -it --rm \
    ${env_params} \
    alpine env

然后出现的命令行是:

docker run -it --rm --env '"MY_VAR"' alpine env

也就是说,您要传递给docker run的参数包括文字双引号.您可以通过使用eval语句来解决此问题(请记住,您需要将脚本修改为export MY_VAR):

That is, the argument you're passing to docker run includes literal double quotes. You can fix that through the use of the eval statement (keeping in mind that you'll need to modify your script to export MY_VAR):

eval docker run -it --rm \
    ${env_params} \
    alpine env | grep MY_VAR

或者(我会更好地争论),只要您使用bash,就可以将env_params变量用作数组:

Alternately (and I would argue preferably) you can use your env_params variable as an array, as long as you're using bash:

env_params=(--env MY_VAR)
env_params+=(--env SOME_OTHER_VAR)

docker run -it --rm \
    "${env_params[@]}" \
    alpine env | grep MY_VAR

这将导致正确的命令行:

Which would result in the correct command line:

docker run -it --rm --env MY_VAR --env SOME_OTHER_VAR alpine env

我想这里的总结是,您的问题最终与"docker run如何解释动态生成的参数"无关,而与"shell变量和内插如何工作"有关.

I guess the summary here is that your issues ultimately have nothing to do with "how docker run interprets dynamically generated arguments", but have everything to do with "how shell variables and interpolation work".

这篇关于Docker如何运行解释动态生成的--env参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆