2

Using redis as my session store in my express.js app. I'm having problems. Narrowed them down to a connection issue.

How do I access a docker environment variable from within an express.js app? I'm using dokku-redis.

It reports that environment variables are automatically set up on the linked app... I've linked my app. running dokku redis:info foo shows that all is linked. I'm trying to pull in REDIS_URL

Thanks, 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'.

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

Edit - sorry, I forgot to add that I have tried process.env

Rob
  • 1,576
  • 3
  • 22
  • 52

2 Answers2

1

you can access environment variables through process.env like the following:

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

also as long as keys in objects do not harm variable naming conventions you can access them like this too:

console.log(process.env.REDIS_URL);

more info in regards to javascript variable naming rules: What characters are valid for JavaScript variable names?

Community
  • 1
  • 1
GottZ
  • 4,824
  • 1
  • 36
  • 46
  • sorry - I should have mentioned in my question that I had tried that. I also logged out the process.env, and nothing of dokku or redis appeared.... So it must be an issue in my configuration – Rob Nov 07 '16 at 14:42
1

How did you link the redis service with your application? You cannot simply do dokku config:set, and if you did, you should unset it and then use dokku redis:link instead. Once you do that, rebuild your app using dokku ps:rebuild APP and you should get process.env.REDIS_URL set.

Jose Diaz-Gonzalez
  • 2,066
  • 1
  • 15
  • 19
  • Hi Jose, thanks. I ran the link command.... it was the first thing I did. Doing a dokku redis:info dbname it shoes the link. So I've tried to unlink it to start over, and get a load of permission denied errors. Will try disabling my app and then see if I can unlink and link the db again – Rob Nov 07 '16 at 16:37
  • So I junked everything.... rebuilt the server. Deployed dokku, and re-installed the plugin. It all works. – Rob Nov 07 '16 at 17:08
  • 1
    @Rob I'm having the exact same problem now... My REDIS_URL env is just returning `undefined`. Do you have any idea what I'm doing wrong? Seems like you just reinstalled everything? Did you use `dotenv`? – FabZbi Jun 14 '20 at 10:59
  • @FabZbi I'm very sorry - this was so long ago, I can't quite remember exactly what I did. However, I think I just junked the lot - reinstalled, and it worked.... I'm pretty sure I used dotenv? – Rob Jun 18 '20 at 09:21