How to connect to a Redis container using Docker Compose?

This docker-compose.yml works fine:

 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 application is waiting for the REDIS_URL configuration. Dockerfile web application:

 ENV REDIS_URL redis:6379 

It seems that the web application cannot connect to Redis:

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

So what is a Redis URL for a web application?

Do I need to open a Redis port (6379)?

EDIT:

Thanks to @Andy Shinn, I see that redis url should be redis://6379 .
Now I get Errno::EINVAL - Invalid argument - connect(2) for 0.0.24.235:6379 . This may be relevant to Redis v 3.2.8. I will try a different version and see if it works.

EDIT # 2:

3.2.8 works great with the application on my local machine, so this is not a problem.

+5
source share
1 answer

The problem is that redis is a "variable" that will be interpolated in the Docker file.

 ENV REDIS_URL redis:6379 

But the Redis URL must start with the redis literal .

So, the fix updates the dockers:

 redis_db: image: redis:3.2.8 

Required Configuration:

 REDIS_URL=redis://redis_db:6379 

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

+7
source

Source: https://habr.com/ru/post/1264538/


All Articles