我正在尝试创建一个docker-compose文件,运行Django Apache服务器和Celery任务,并使用RabbitMQ作为消息代理。我的问题是Celery无法连接到RabbitMQ,我会收到以下错误信息:
[2021-02-18 08:11:44,769: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@rabbitmq:5672//: [Errno 111] Connection refused.
这是我第一次构建Docker镜像,所以可能有很多错误。下面是我的docker-compose文件:
[2021-02-18 08:11:44,769: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@rabbitmq:5672//: [Errno 111] Connection refused.
这是我第一次构建Docker镜像,所以可能有很多错误。下面是我的docker-compose文件:
version: "3.8"
services:
server: &server
build:
context: .
environment:
- PYTHONUNBUFFERED=1
- CELERY_BROKER=amqp://admin:password@rabbitmq:5672//
volumes:
- .:/var/www/html/public
ports:
- 80:80
command:
["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
depends_on:
- rabbitmq
- celery_beat
networks:
- main
rabbitmq:
container_name : rabbitmq
image: rabbitmq:3.8
hostname: rabbitmq
ports:
- 5672:5672
networks:
- main
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=password
celery_worker:
<<: *server
hostname: rabbitmq
command: celery -A public worker -l INFO
ports: []
networks:
- main
depends_on:
- rabbitmq
environment:
#- RABBITMQ_DEFAULT_USER=admin
#- RABBITMQ_DEFAULT_PASS=password
- CELERY_BROKER=amqp://admin:password@rabbitmq:5672//
networks:
main:
Dockerfile:
FROM ubuntu:latest
ENV PYTHONUNBUFFERED=1
RUN apt-get -y update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y apache2 python3.8 python3-pip libapache2-mod-wsgi-py3
RUN pip3 install --upgrade pip
ADD requirements.txt .
RUN pip install -r requirements.txt
ADD ./scraper_project.conf /etc/apache2/sites-available/scraper_project.conf
ADD . /var/www/html/public
WORKDIR /var/www/html/public
RUN chown -R :www-data .
RUN chmod -R 775 .
EXPOSE 80
RUN a2ensite scraper_project
RUN a2dissite 000-default
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Celery配置:
CELERY_BROKER_URL = os.environ['CELERY_BROKER']