django - Redis+Docker+Django - 错误111连接被拒绝

标签 django redis docker

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

docker exec -it <redis_container_name> redis-cli
ping

并看到响应是 PONG

这是我的 settings.py 中的 Celery 设置:

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 容器设置:

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

我还记得将 redis 容器与我的 web 容器链接起来。我可以很好地启动服务器,但是当我尝试将任何内容上传到站点时出现连接被拒绝错误。到底出了什么问题?

编辑:我记得使用 VBoxManage 进行端口转发,这样我就可以转到我的浏览器并在 localhost:8000 访问我的网站,所以它似乎没有就像我需要为我的 settings.py 使用 VM 的 IP 而不是 localhost

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

最佳答案

由于将 Celery 从 v3.1 更新到 v4 并根据此 tutorial,我遇到了类似的问题需要在 settings.py

中将 BROKER_URL 更改为 CELERY_BROKER_URL

settings.py部分

CELERY_BROKER_URL = 'redis://cache:6379/0'
CELERY_RESULT_BACKEND = 'redis://cache:6379/0'

docker-compose.yml部分

version: '2'
services:
  web:
    container_name: django-container
    *******
    other options
    *******

    depends_on:
      - cache
      - db

  cache:
    container_name: redis-container
    restart: always
    image: redis:latest

关于django - Redis+Docker+Django - 错误111连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32615370/

相关文章:

python - Django 表单 : "Select a valid choice. That choice is not one of the available choices."

python - 无法将字典更新序列元素 #0 转换为序列

docker - ssh-agent 不在 jenkins 管道上工作

Django testing client.login() : Your database backend doesn't behave properly when autocommit is off. 使用前开启 'atomic'

authentication - Node.js 发送后无法设置 header

windows - Windows 上 Redis 的日志文件

node.js - 在redis中有效地缓存搜索结果

apache - 502 Bad Gateway nginx/1.11.5 在 docker 中运行

docker - 包括本地卷名的无效字符。如果您打算传递主机目录,请使用绝对路径

django - 如何在 Django 中上传之前预览图像