在应用环境中使用链接的容器环境变量? [英] Using linked container environment variables in application environment?

查看:184
本文介绍了在应用环境中使用链接的容器环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序使用一个名为 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屋!

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