redis connection error within docker container

泪湿孤枕 提交于 2021-01-29 19:59:32

问题


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

ValueError: Redis URL must specify one of the following schemes (redis://, rediss://, unix://)

When grabbing from environmental variable (os.getenv()) it doesn't work, but somehow when hard-coding in redis://redis:6379/0, it seems to work. However, this is not really a long-term solution in my opinion. 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.


回答1:


Found out it there were mysterious \ at the ends of the string, so I used .strip() and it works using the .getenv() method. I think it has something to do with how docker-compose passes the variable to flask; my best guess.



来源:https://stackoverflow.com/questions/63269031/redis-connection-error-within-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!