redis connection error within docker container

I’m trying to setup a light-weight RQ task queue app running in Docker. I keep on getting this error:

redis.exceptions.ConnectionError: Error -2 connecting to redis:6379. Name or service not known.

or

ValueError: Redis URL must specify one of the following schemes (redis://, rediss://, unix://)
I get the first error if I hardcode redis://redis:6379 and the second when grabbing from environmental variable (os.getenv()). I looked at: link1 and link2, but there wasn’t really a solution, the thread went dead.

My redis connection code is like so:

import os
import redis
from rq import Worker, Queue, Connection

listen = ['default']
redis_url = os.getenv('REDIS_URL', 'redis://localhost:6379')

if __name__ == '__main__':
    print(f"redis url: {redis_url}")
    with Connection(redis.from_url(redis_url)):
        worker = Worker(list(map(Queue, listen)))
        worker.work()

When it prints the redis_url it is correct; I set the environmental var, REDIS_URL to redis://redis:6379 where redis is the hostname in docker-compose.yml.

  redis:
    image: redis:alpine
    expose:
      - '6379'

Appreciate any help. Thanks.

Go to Source
Author: Spencer Trinh