英文:
How to connect celery to rabbitmq in django from docker
问题
我学习Docker,但不明白为什么Celery无法连接到RabbitMQ。我已为RabbitMQ和Django创建了2个镜像,以下是我的Docker Compose配置:
version: "3.0"
services:
# WEB
django:
build: .
volumes:
- media_volume:/website/journal_website/media
- static_volume:/website/journal_website/static
- database_volume:/website/journal_website/database
ports:
- "8000:8000"
depends_on:
- rabbit
# RabbitMQ
rabbit:
hostname: rabbit
container_name: rabbitmq
image: rabbitmq:3.12-rc-management
environment:
- RABBITMQ_DEFAULT_USER=simple_user
- RABBITMQ_DEFAULT_PASS=simple_password
ports:
# AMQP协议端口
- "5672:5672"
# HTTP管理界面
- "15672:15672"
restart: always
volumes:
media_volume:
static_volume:
database_volume:
我的settings.py
中的代理配置:
CELERY_BROKER_URL = 'amqp://localhost:5672'
celery.py
文件:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings")
celery_application = Celery("website")
celery_application.config_from_object("django.conf:settings", namespace="CELERY")
celery_application.autodiscover_tasks()
entrypoint.sh
文件,它在Dockerfile
中用作入口点:
#!/bin/bash
# 启动Celery
celery -A website worker -l info
# 运行Django服务器
python3.11 manage.py runserver 127.0.0.1:8000
以及Dockerfile
:
# 安装Python和Ubuntu 20.04中的所有依赖项的众多命令
# 运行Django服务器
EXPOSE 8000
ENTRYPOINT ["/entrypoint.sh"]
我在Windows 10上使用Docker Desktop,当我运行docker compose up
或在桌面应用程序中运行容器时,我收到如下错误消息:“[Date: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.”,我已经检查过localhost
是127.0.0.1
。我应该怎么做才能正确运行Celery?
英文:
I learn docker and don't understand why celery can't connect to rabbitmq. I've created 2 images for rabbitmq and django, here is my docker-compose:
version: "3.0"
services:
# WEB
django:
build: .
volumes:
- media_volume:/website/journal_website/media
- static_volume:/website/journal_website/static
- database_volume:/website/journal_website/database
ports:
- "8000:8000"
depends_on:
- rabbit
# RabbitMQ
rabbit:
hostname: rabbit
container_name: rabbitmq
image: rabbitmq:3.12-rc-management
environment:
- RABBITMQ_DEFAULT_USER=simple_user
- RABBITMQ_DEFAULT_PASS=simple_password
ports:
# AMQP protocol port
- "5672:5672"
# HTTP management UI
- "15672:15672"
restart: always
volumes:
media_volume:
static_volume:
database_volume:
my broker configuration in settings.py
in django:
CELERY_BROKER_URL = 'amqp://localhost:5672'
celery.py
file:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings")
celery_application = Celery("website")
celery_application.config_from_object("django.conf:settings", namespace="CELERY")
celery_application.autodiscover_tasks()
entrypoint.sh
that's used as entrypoint in Dockerfile
:
#!/bin/bash
# Start Celery
celery -A website worker -l info
# Run Django server
python3.11 manage.py runserver 127.0.0.1:8000
and Dockerfile
:
# Many many commands to install python and all dependencies in ubuntu 20.04
# Run Django server
EXPOSE 8000
ENTRYPOINT ["/entrypoint.sh"]
I use Docker Desktop on Windows 10, when I run docker compose up
or run container in desktop application I get such error: [Date: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
, I've checked that localhost
is 127.0.0.1
. What should I do to run celery properly?
答案1
得分: 1
可能需要将您的URL更改为:
amqp://rabbit:5672
英文:
Probably you have to change your url to:
amqp://rabbit:5672
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论