Redis + Docker + Django-错误111连接被拒绝 [英] Redis+Docker+Django - Error 111 Connection Refused

查看:159
本文介绍了Redis + Docker + Django-错误111连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试将Redis用作我的使用Docker Compose的Django项目的Celery的代理。我无法弄清楚我到底做错了什么,但是尽管控制台日志消息告诉我Redis正在运行并接受连接(实际上,当我执行 docker ps ,我可以看到容器正在运行),但仍然收到有关拒绝连接的错误。我什至

I'm trying to use Redis as a broker for Celery for my Django project that uses Docker Compose. I can't figure out what exactly I've done wrong, but despite the fact that the console log messages are telling me that Redis is running and accepting connections (and indeed, when I do docker ps, I can see the container running), I still get an error about the connection being refused. I even did

docker exec -it <redis_container_name> redis-cli
ping

并看到响应为 PONG

以下是我 settings.py 中的芹菜设置:

Here are the Celery settings in my settings.py:

BROKER_URL = 'redis://localhost:6379/0'
BROKER_TRANSPORT = 'redis'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ENABLE_UTC = True
CELERY_TIMEZONE = "UTC"

这是我的 docker-compose.yml

redis:
    image: redis
    ports:
        - "6379:6379"

我记得链接 redis 容器以及我的 web 容器。我可以正常启动服务器,但是在尝试将任何内容上传到站点时出现连接拒绝错误。究竟出了什么问题?

I remembered to link the redis container with my web container as well. I can start up the server just fine, but I get the connection refused error when I try to upload anything to the site. What exactly is going wrong?

编辑:我记得使用VBoxManage进​​行端口转发,以便可以转到浏览器并访问我的网站在 localhost:8000 ,所以我似乎不需要使用VM的IP来代替 localhost settings.py

EDIT: I remembered to use VBoxManage to port forward such that I can go to my browser and access my site at localhost:8000, so it doesn't seem like I need to use the VM's IP instead of localhost for my settings.py.

编辑2 :如果我用IP地址替换设置中的 localhost docker-machine VM或Redis容器的IP地址,然后发生的事情是,当我上传文件时,我真的很快在网站上收到错误的成功消息,但实际上什么也没有上传。基础上载函数 insertIntoDatabase()使用 delay

EDIT 2: If I replace localhost in the settings with either the IP address of the docker-machine VM or the IP address of the Redis container, then what happens is that I really quickly get a false success message on my website when I upload a file, but then nothing actually gets uploaded. The underlying upload function, insertIntoDatabase(), uses delay.

推荐答案

Django是否在与Redis容器链接的单独容器中运行?如果是这样,您应该具有一些环境变量以及Django应该用来连接到Redis容器的Ip和端口。将BROKER_URL设置为使用redis Ip和port env vars,您就应该做生意。

Is Django running in a seperate container that is linked to the Redis container? If so, you should have some environment variables with the Ip and port that Django should use to connect to the Redis container. Set BROKER_URL to use the redis Ip and port env vars and you should be in business. Ditto for RESULT_BACKEND.

环境变量的参考文档在这里: Docker Compose文档

Reference docs for the env vars are here: Docker Compose docs

以下是一些示例代码,说明了我们如何在OfferUp的一个项目中使用自动添加的env var:

Here's some example code for how we use the automatically added env vars in one of our projects at OfferUp:

BROKER_TRANSPORT = "redis"
_REDIS_LOCATION = 'redis://{}:{}'.format(os.environ.get("REDIS_PORT_6379_TCP_ADDR"), os.environ.get("REDIS_PORT_6379_TCP_PORT"))
BROKER_URL = _REDIS_LOCATION + "/0"
CELERY_RESULT_BACKEND = _REDIS_LOCATION + "/1"

这篇关于Redis + Docker + Django-错误111连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆