docker容器内的redis连接错误 [英] redis connection error within docker container
问题描述
我正在尝试设置一个在Docker中运行的轻量级RQ任务队列应用程序.我不断收到此错误:
I'm trying to setup a light-weight RQ task queue app running in Docker. I keep on getting this error:
ValueError:Redis URL必须指定以下方案之一(redis://,rediss://,unix://)
从环境变量( os.getenv()
)抓取时不起作用,但是以某种方式在 redis://redis:6379/0
中进行硬编码时>,似乎有效.但是,我认为这并不是一个长期的解决方案.我看了看: link1 和
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.
我的redis连接代码如下:
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()
当打印 redis_url
时,它是正确的;我将环境变量 REDIS_URL
设置为 redis://redis:6379
,其中redis是docker-compose.yml中的主机名.
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'
感谢任何帮助.谢谢.
推荐答案
发现它的字符串末尾有一个神秘的 \
,所以我使用了 .strip()
,并使用 .getenv()
方法工作.我认为这与docker-compose如何将变量传递给flask有关.我最好的猜测.
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.
这篇关于docker容器内的redis连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!