docker运行“ --env-file”的解决方法提供的文件未按预期评估 [英] Workaround to docker run "--env-file" supplied file not being evaluated as expected

查看:1074
本文介绍了docker运行“ --env-file”的解决方法提供的文件未按预期评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前用于运行docker容器的设置如下:

My current setup for running a docker container is on the lines of this:


  1. 我有一个 main.env 文件:

  1. I've got a main.env file:



# Main
export PRIVATE_IP=\`echo localhost\`
export MONGODB_HOST="$PRIVATE_IP"
export MONGODB_URL="mongodb://$MONGODB_HOST:27017/development"




  1. 在我的服务文件(新贵)中,我将此文件的来源为。 /path/to/main.env

然后我调用 docker run 对于要在容器内的每个环境变量,使用多个 -e 。在这种情况下,我会称呼类似的命令: docker run -e MONGODB_URL = $ MONGODB_URL ubuntu bash

I then call docker run with multiple -e for each of the environment variables I want inside of the container. In this case I would call something like: docker run -e MONGODB_URL=$MONGODB_URL ubuntu bash

然后,我希望容器内的 MONGODB_URL 等于 mongodb:// localhost:27017 / development 。请注意,实际上 echo localhost curl 替换为亚马逊api的实际 PRIVATE_IP

I would then expect MONGODB_URL inside of the container to equal mongodb://localhost:27017/development. Notice that in reality echo localhost is replaced by a curl to amazon's api for an actual PRIVATE_IP.

当您开始拥有越来越多的环境变量时,这变得有些笨拙需要给你的容器。这里有一个很好的观点,那就是环境变量需要在运行时 进行解析,例如调用 curl 或通过引用其他环境变量。

This becomes a bit unwieldy when you start having more and more environment variables you need to give your container. There is a fine point to see here which is that the environment variables need to be resolved at run time, such as with a call to curl or by referring to other env variables.

我希望使用的解决方案是:

The solution I was hoping to use is:


  1. 使用-env-file 参数调用 docker run

  1. calling docker run with an --env-file parameter such as this:



# Main
PRIVATE_IP=\`echo localhost\`
MONGODB_HOST="$PRIVATE_IP"
MONGODB_URL="mongodb://$MONGODB_HOST:27017/development"




  1. 然后我的 docker run 命令将大大缩短为 docker run --env-file = / path / to / main.env ubuntu bash (请记住,通常我会有12-15个环境变量。

  1. Then my docker run command would be significantly shortened to docker run --env-file=/path/to/main.env ubuntu bash (keep in mind usually I've got around 12-15 environment variables.

这是我命中目标的地方问题是容器内部没有任何变量能够按预期解析,相反,我最终得到:

This is where I hit my problem which is that inside the container none of the variables resolve as expected. Instead I end up with:


  • PRI VATE_IP =ʻecho localhost`

  • MONGODB_HOST = $ PRIVATE_IP

  • MONGODB_URL = mongodb:// $ MONGODB_HOST:27017 / development

我可以通过以下操作来规避这一点:

I could circumvent this by doing the following:


  1. 采购 main.env 文件。

  2. 创建一个仅包含我想要的变量名称的文件(这意味着docker会搜索

  3. 然后使用该文件作为的参数调用 docker run 环境文件。这会起作用,但是这意味着我需要维护两个文件而不是一个文件,而且对当前情况的改善确实不会太大。

  1. Sourcing the main.env file.
  2. Creating a file containing just the names of the variables I want (meaning docker would search for them in the environment).
  3. Then calling docker run with this file as an argument to --env-file. This would work but would mean I would need to maintain two files instead of one, and really wouldn't be that big of an improvement of the current situation.

我想让变量按预期方式解析。

What I would prefer is to have the variables resolve as expected.

我能找到的最接近我的问题是:
< a href = https://stackoverflow.com/questions/25177402/12factor-config-approach-with-docker>使用Docker的12factor配置方法

The closest question to mine that I could find is: 12factor config approach with Docker

推荐答案

-env -env-file 设置变量是且不替换嵌套变量。

Both --env and --env-file setup variables as is and do not replace nested variables.

Solomon Hykes 讨论了在运行时配置容器以及各种方法。一个对您有用的方法是将main.env从主机批量安装到容器中并进行采购。

Solomon Hykes talks about configuring containers at run time and the the various approaches. The one that should work for you is to volume mounting the main.env from host into the container and sourcing it.

这篇关于docker运行“ --env-file”的解决方法提供的文件未按预期评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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