Search code examples
dockerredisdocker-composepython-rq

rq in Docker: "Could not resolve a Redis connection" with an otherwise working redis connection


I feel like I'm doing what every other question on this has an accepted answer suggesting. But I am unable to get a Flask app using rq to successfully connect to redis despite successfully connecting to redis using a bare Redis instance.

In my app code from my web service:

from rq import Queue
from rq import Queue
from redis import Redis

redis = Redis(host="redis", db=0, socket_connect_timeout=10, socket_timeout=10)

# ...

        visits = redis.incr("counter")
        print(f"visits: {visits}")

        q = Queue(redis);
        q.enqueue(my_worker.work)

and logs:

visits: 11
rq.connections.NoRedisConnectionException: Could not resolve a Redis connection

The visits counter is working properly, so clearly there is a working Redis connection. Yet the Queue instance is unable to use it. No difference if I initialize a separate Redis instance for the Queue (or leave the counter stuff out entirely).

I'm running this stack using docker stack deploy with a docker-compose.yml that is not too modified from the beginner's tutorial:

version: "3"
services:
  web:
    image: ozydingo/workers-web:latest
    deploy: #...
    ports:
      - "80:80"
    networks:
      - webnet
  worker:
    image: ozydingo/workers-worker:latest
    deploy: #...
    networks:
      - webnet
  redis:
    image: redis
    ports:
      - "6379:6379"
    volumes:
      - "/home/docker/data:/data"
    deploy: #...
    networks:
      - webnet
#...
networks:
  webnet:

How should I interpret an error from Queue claiming no Redis connection could be resolved when my redis instance is telling me otherwise?


Solution

  • If I take for granted that the official documentation should be followed, the correct syntax to create a queue object is:

    my_queue = Queue(connection=my_redis_connection_object)
    

    So change your line

    # /!\ Wrong /!\
    q = Queue(redis)
    

    to the following

    q = Queue(connection=redis)
    

    --

    Supplementary note: Nothing to do with your problem, but you have a duplicate import for Queue at the top of your file.