如何在Django中通过Docker将Celery连接到RabbitMQ。

huangapple go评论94阅读模式
英文:

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.”,我已经检查过localhost127.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

huangapple
  • 本文由 发表于 2023年7月14日 01:43:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定