在应用环境中使用链接的容器环境变量? [英] Using linked container environment variables in application environment?
问题描述
我有一个应用程序使用一个名为 REDIS_URL
的环境变量。一个典型的 REDIS_URL
将是 redis://172.17.0.5:6379/0
。我想要根据容器链接填充 REDIS_URL
:
I have an application which uses an environment variable named REDIS_URL
. A typical REDIS_URL
would be redis://172.17.0.5:6379/0
. I'd like to be able to populate REDIS_URL
based on container linking:
docker run --name redis -d redis
docker run --name firehose --link redis:redis -e REDIS_URL="redis://$REDIS_PORT_6379_TCP_ADDR:$REDIS_PORT_6379_TCP_PORT/0" -d firehose/server
但是,根据我如何逃避环境变量,它们是在docker运行时在我的shell中进行评估,是空白的( redis://:/ 0
),或以文字字符串的形式传递( redis:// $ REDIS_PORT_6379_TCP_ADDR:$ REDIS_PORT_6379_TCP_PORT / 0
)。
But depending on how I escape the environment variables, they are either evaluated in my shell at docker run time and are blank (redis://:/0
), or passed as literal strings (redis://$REDIS_PORT_6379_TCP_ADDR:$REDIS_PORT_6379_TCP_PORT/0
).
如何根据conatiner链接填充我的 REDIS_URL
应用程序环境变量? / p>
How can I populate my REDIS_URL
application environment variable based on conatiner linking?
推荐答案
$ REDIS_PORT_6379_TCP_ADDR
和 $ REDIS_PORT_6379_TCP_PORT
变量在 docker运行
命令执行时不知道,因此无法从主机构建它。
The $REDIS_PORT_6379_TCP_ADDR
and $REDIS_PORT_6379_TCP_PORT
variables are not known at the time the docker run
command is executed so there's no way to construct it from the host.
但是,有一个解决方法。在 Dockerfile
中, firehose / server
图像必须有一个 CMD
或 ENTRYPOINT
指定运行映像时执行的命令。您可以在该命令周围放置一个包装器,构建 REDIS_URL
变量。如下所示:
However, there is a workaround. In the Dockerfile
for the firehose/server
image there must be a CMD
or ENTRYPOINT
that dictates what command is executed when the image is run. You can put a wrapper around that command that will construct the REDIS_URL
variable. Something like this:
#!/bin/sh
export REDIS_URL="redis://${REDIS_PORT_6379_TCP_ADDR}:${REDIS_PORT_6379_TCP_PORT}/0"
<run command>
使用包装器脚本作为 CMD
或 ENTRYPOINT
在 Dockerfile
中。
Use the wrapper script as the CMD
or ENTRYPOINT
in the Dockerfile
.
这篇关于在应用环境中使用链接的容器环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!