如何从Docker在Django中连接celery到rabbitmq

w6lpcovy  于 2023-08-05  发布在  RabbitMQ
关注(0)|答案(1)|浏览(179)

我学习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 protocol port
      - "5672:5672"
      # HTTP management UI
      - "15672:15672"
    restart: always
      

volumes:
  media_volume:
  static_volume:
  database_volume:

字符串
我在django的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()


Dockerfile中用作入口点的entrypoint.sh

#!/bin/bash

# Start Celery
celery -A website worker -l info

# Run Django server
python3.11 manage.py runserver 127.0.0.1:8000


Dockerfile

# Many many commands to install python and all dependencies in ubuntu 20.04

# Run Django server
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 ?

pxiryf3j

pxiryf3j1#

您可能需要将URL更改为:amqp://rabbit:5672

相关问题