Docker环境变量,dokku-redis

时间:2016-11-07 13:07:26

标签: node.js express docker environment-variables dokku

在我的express.js应用中使用redis作为我的会话商店。我有问题。将它们缩小到连接问题。

如何从express.js应用中访问docker环境变量?我正在使用dokku-redis。

它报告环境变量是自动设置在链接的应用程序上...我已经链接了我的应用程序。运行dokku redis:info foo显示所有链接。我正试图拉入REDIS_URL

谢谢,Rob

https://github.com/dokku/dokku-redis

------------
a redis service can be linked to a
container this will use native docker
links via the docker-options plugin
here we link it to our 'playground' app
NOTE: this will restart your app
dokku redis:link lolipop playground

the following environment variables will be set automatically by docker (not on the app itself, so they won’t be listed when calling dokku config)

   DOKKU_REDIS_LOLIPOP_NAME=/lolipop/DATABASE
   DOKKU_REDIS_LOLIPOP_PORT=tcp://172.17.0.1:6379
   DOKKU_REDIS_LOLIPOP_PORT_6379_TCP=tcp://172.17.0.1:6379
   DOKKU_REDIS_LOLIPOP_PORT_6379_TCP_PROTO=tcp
   DOKKU_REDIS_LOLIPOP_PORT_6379_TCP_PORT=6379
   DOKKU_REDIS_LOLIPOP_PORT_6379_TCP_ADDR=172.17.0.1

and the following will be set on the linked application by default

REDIS_URL=redis://lolipop:SOME_PASSWORD@dokku-redis-lolipop:6379

NOTE: the host exposed here only works internally in docker containers. If
you want your container to be reachable from outside, you should use 'expose'.

------------------------------

编辑 - 抱歉,我忘了添加我已经尝试过process.env

2 个答案:

答案 0 :(得分:1)

您可以通过process.env访问环境变量,如下所示:

console.log(process.env["DOKKU_REDIS_LOLIPOP_NAME"]);
console.log(process.env["DOKKU_REDIS_LOLIPOP_PORT"]);
console.log(process.env["REDIS_URL"]);

只要对象中的键不会损害变量命名约定,您也可以像这样访问它们:

console.log(process.env.REDIS_URL);

有关javascript变量命名规则的更多信息:What characters are valid for JavaScript variable names?

答案 1 :(得分:1)

您是如何将redis服务与您的应用程序链接的?您不能简单地执行dokku config:set,如果您这样做,则应取消设置,然后再使用dokku redis:link。完成后,使用dokku ps:rebuild APP重建您的应用,然后设置process.env.REDIS_URL