OStack程序员社区-中国程序员成长平台

标题: bash - 我如何使用bash将环境传递给docker? [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-3 10:12
标题: bash - 我如何使用bash将环境传递给docker?

以下在docker中可以正常工作:

docker run -i -t -rm -e a="hello world" b=world ubuntu /bin/bash

它的作用是将值“hello world”的env var a和值“world”的env var b传递到docker容器中。

事情是,我需要从脚本生成它。

使此方法在没有空间的环境变量中工作非常容易:
ENV_VARS='-e a=helloworld b=world'
docker run -i -t -rm $ENV_VARS ubuntu /bin/bash

但是,一旦env var中有空格,我就会被抽空:
ENV_VARS='-e a="hello world" b=world'
docker run -i -t -rm $ENV_VARS ubuntu /bin/bash

Unable to find image 'world"' (tag: latest) locally
2014/01/15 16:28:40 Invalid repository name (world"), only [a-z0-9-_.] are allowed

如何使以上示例起作用?我也尝试过数组,但无法使它们工作。



Best Answer-推荐答案


Bash数组旨在解决此类问题

第一步是声明数组:

docker_env=(-e "a=hello world" "b=world")

通过它可以以编程方式填充更多环境变量,例如:
docker_env+=("c=foo bar")

最后运行它:
docker run -i -t -rm "${docker_env[@]}" ubuntu /bin/bash

关于bash - 我如何使用bash将环境传递给docker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21151216/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4