简体   繁体   中英

Can't connect celery with rabbitmq server on docker

I'm trying to create docker-compose file that will run django apache server with celery tasks, and using rabbitmq as message brooker. My problem is that celery can't connect to rabbitmq. I'm getting that error:

[2021-02-18 08:11:44,769: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@rabbitmq:5672//: [Errno 111] Connection refused.

Also this is my first time building docker images so i probably did a lot of mistakes there.

My docker-compose file:

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 settings:

CELERY_BROKER_URL = os.environ['CELERY_BROKER']

Fixed. Problem was that i set up hostname in celery worker. If someone has similar problem just remove hostname: rabbitmq on celery_worker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM