How to connect to a Redis container using Docker Compose?
Asked Answered
F

3

38

This docker-compose.yml seems to work OK:

version: '3'
services:
  web:
    image: web-app
    command: bundle exec rackup
    ports:
     - "9292:9292"
    links:
      - redis
  redis:
    image: redis

Command:

docker build -t web-app .; docker-compose up

The Web App is expecting a REDIS_URL config. Web App Dockerfile:

ENV REDIS_URL redis:6379

It seems that the Web App cannot connect to the Redis:

Redis::CannotConnectError - Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED):

So, what is the Redis URL for the Web App?

Do I need to expose the Redis port (6379)?

EDIT:

Thanks to @Andy Shinn, I see the redis url should be redis://6379.
Now I get Errno::EINVAL - Invalid argument - connect(2) for 0.0.24.235:6379. This may have to do with Redis v 3.2.8. I'll try another version and see if it works.

EDIT #2:

3.2.8 works fine with the app on my local machine, so that is not the issue.

Fannyfanon answered 21/2, 2017 at 20:2 Comment(8)
Is redis running at that point? What happens if you start them all, and then restart the web after a bit -> that way you know that redis has been started.Miscue
That error says the web service is trying to connect to redis at the local IP (127.0.0.1) and not the REDIS_URL which should be the redis DNS name from the link. What kind of application is this? Is it designed to use the REDIS_URL? It would be helpful to post a bit more about the application (Dockerfile, how it uses the REDIS_URL, etc).Renae
Yes, it is configured to use the REDIS_URL. It's a Sinatra app. Other configs work, and production has a REDIS_URL.Fannyfanon
@Miscue - Does not seem to be a timing issue. Redis starts before the web app.Fannyfanon
Is 0.0.24.235 the IP it's attempting to reach now, or a copy/paste error?Ernestineernesto
I'd also change link to depends_on, the link syntax is being phased out.Ernestineernesto
@Ernestineernesto - That is the correct IP. I guess thats how links works...? I'll try depends_on.Fannyfanon
That IP doesn't look right at all to me, if it was 10.0.24.235 it would make sense. Get the redis container ip from a docker inspect $redis_container_id (use docker ps to get the id) and compare to what the app is trying to use.Ernestineernesto
F
57

The issue is that redis is "variable" that will be interpolated in the Dockerfile.

ENV REDIS_URL redis:6379

But the Redis URL should start with the literal redis.

So, the fix is updating the docker compose:

redis_db:
  image: redis:3.2.8

The desired config is:

REDIS_URL=redis://redis_db:6379

Thanks to BMitch and Andy Shinn for helping me figure this out.

Fannyfanon answered 22/2, 2017 at 0:37 Comment(4)
FWIW, Mongo URL's can have the same issue because they start with mongodb://.Fannyfanon
do you have any idea how REDIS_URL looks like if i set a password?Expressive
@calvin redis://user:secret@localhost:6379/0?foo=bar&qux=bazCuboid
this saved a lot of time. thank youAcarpous
T
1

I had the same issue. Resolved it by just specifying the network. So in your case it'll be

version: '3'
services:
  web:
    image: web-app
    command: bundle exec rackup
    ports:
     - "9292:9292"
    links:
      - redis
    networks:
      - mynetwork
  redis:
    image: redis
    networks:
      - mynetwork
networks:
  mynetwork:

And in this case redis host is redis

Thanksgiving answered 3/6 at 10:31 Comment(0)
B
0

The create client config is updated in the new version

doc for the same https://github.com/redis/node-redis/blob/master/docs/client-configuration.md

Bohunk answered 21/5, 2022 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.