Error 99 connecting to localhost:6379. Cannot assign requested address
Asked Answered
T

3

48

Setup: I have a virtual machine and in the virtual machine running three containers (an nginx proxy, a very minimalistic flask app and redis). Flask should be serving on port 5000 while redis on 6379.

Each of these containers are up and running just fine as stand a lone services, but also available via docker compose as a service.

In the flask app, my aim is to connect to redis and query for some keys.

The nginx container exposes port 80, flask port 5000 and redis port 6379.

In the flask app I have a function that tries to create a redis client

db = redis.Redis(host='localhost', port=6379, decode_responses=True)

Running the flask app I am getting an error that the port cannot be used

redis.exceptions.ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address.

I am lost of clarity what could be causing this problem and any ideas would be appreciated.

Toddy answered 3/3, 2019 at 3:26 Comment(0)
I
100

In the flask app I have a function that tries to create a redis client

db = redis.Redis(host='localhost', port=6379, decode_responses=True)

When your flask process runs in a container, localhost refers to the network interface of the container itself. It does not resolve to the network interface of your docker host.

So you need to replace localhost with the IP address of the container running redis.

In the context of a docker-compose.yml file, this is easy as docker-compose will make service names resolve to the correct container IP address:

version: "3"
services:
  my_flask_service:
    image: ...
  my_redis_service:
    image: ...

then in your flask app, use:

db = redis.Redis(host='my_redis_service', port=6379, decode_responses=True)
Illustrational answered 3/3, 2019 at 12:45 Comment(3)
Thanks, this works. It wasn't so apparent, that in the docker environment the service name applies and yes should have noted that in the setting given localhost implies the container itself and not the VM.Toddy
Nice one. Helped me as wellTrincomalee
After applying these changes I've got the error: ConnectionError: Error -2 connecting to lightquery-backend_redis-server_1:6379. Name or service not known. I'm trying to connect from one container to the redis' one. lightquery-backend_redis-server_1 is the worker's one, as I saw using docker ps and taking the name: a2f3928d5a27 redis:alpine "docker-entrypoint.s…" 20 hours ago Up 19 hours 0.0.0.0:6379->6379/tcp lightquery-backend_redis-server_1Hinze
A
17

I had this same problem, except the service I wanted my container to access was remote and mapped via ssh tunnel to my Docker host. In other words, there was no docker-compose service for my code to find. I solved the problem by explicitly telling redis to look for my local host as a string:

pyredis.Redis(host='docker.for.mac.localhost', port=6379)
Alienable answered 27/8, 2019 at 19:26 Comment(3)
Thanks! This host also works in RedisInsights ui on mac.Compensatory
don't you think by making this change we are making it as hard dependency on Mac? this won't work if we want to run the same code in linux machine!.Multiplier
Just set the path as an environment variable or discover the OS at runtime within python to account for thisAlienable
C
3

Anyone using only docker to run a container, you can add --network=host in the command like docker run --network=host to make docker use the network of the host while running the container.

You can also use a host network for a swarm service, by passing --network host to the docker service create command.
Make sure you don't publish any port while doing this. like -p 80:8000

I am not sure if Docker compose supports this.

N.b. this is only supported in linux.

Composer answered 30/8, 2021 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.