使用Django在Docker上设置RabbitMQ

时间:2019-04-06 19:49:46

标签: django docker rabbitmq

我正在尝试使用Django建立Docker容器。我的Docker文件是:

FROM python:3.7-alpine
MAINTAINER Freshness Productions

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client
RUN apk add --update --no-cache --virtual .tmp-build-deps \
    gcc libc-dev linux-headers postgresql-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN adduser -D user
USER user

RUN rabbitmqctl add_user test testpass1
RUN rabbitmqctl add_vhost myvhost
RUN rabbitmqctl set_permissions -p myvhost test ".*" ".*" ".*"
RUN rabbitmq-server

我的docker-compose.yml是:

version: "3"

services:
  app:
    build:
      context: .
    image: &app app
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py wait_for_db &&
             python manage.py migrate &&
             python manage.py runserver 0.0.0.0:8000
             export C_FORCE_ROOT='true'
             celery -A app beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler"
    env_file: &envfile
      - env.env
    depends_on:
      - db
      - broker

  db:
    image: postgres:10-alpine
    environment:
      - POSTGRES_DB=app
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=supersecretpassword

  worker:
    build: .
    image: *app
    restart: "no"
    env_file: *envfile
    command: sh -c "celery -A app worker --loglevel=info"
    volumes:
      - ./app:/app
    depends_on:
      - broker

  broker:
    image: rabbitmq:3
    env_file: *envfile
    ports:
      - 5672:5672

我看不到我需要做什么-除了我在docker-compose中使用两个不同的图像之外?有关系吗我收到错误消息:

ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.

当我尝试运行docker-compose时

1 个答案:

答案 0 :(得分:2)

在这里您可以找到与RabbitMQ,Python Django,MYQL以及Celery和Celery Beat完美配合的配置。

如果愿意,不要忘记投票。谢谢。

docker-compose.yml

version: "3.7"
services:

django:
    container_name: django_python
    image: python:3.6
    command: bash -c "pip3 install -r requirements.txt && python manage.py runserver 0.0.0.0:8000"
    volumes:
        - ${APP_PATH}var/www/api.mylaser.fr/mylazer_backend:/app
        - ${APP_PATH}var/www/static.mylaser.fr:/app/static
    depends_on:
        - mysql
    working_dir: /app
    environment:
        - PYTHONUNBUFFERED=1
        - MYSQL_HOST=mysql
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - CELERY_BROKER=${CELERY_BROKER}
mysql:
    container_name: python_mysql
    image: mariadb:latest
    #ports:
    #    - "3306:3306"
    volumes:
        - ${APP_PATH}var/mysql:/var/lib/mysql
        - ${APP_PATH}etc/mysql:/etc/mysql
    environment:
        - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - TZ=Europe/Paris
rabbitmq:
    image: rabbitmq:3.8.2-management
    container_name: rabbitmq
    volumes:
        - ${APP_PATH}etc/rabbitmq:/etc/rabbitmq/:rw
        - ${APP_PATH}var/rabbitmq:/var/lib/rabbitmq/:rw
        - ${APP_PATH}var/log/rabbitmq:/var/log/rabbitmq/:rw
    environment:
        HOSTNAME: ${RABBITMQ_HOSTNAME}
        RABBITMQ_ERLANG_COOKIE: ${RABBITMQ_ERLANG_COOKIE}
        RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
        RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
    ports:
        - ${RABBITMQ_PORT}:5672
        - ${RABBITMQ_MGT_PORT}:15672
worker:
    image: python:3.6
    container_name: worker
    command: bash -c "pip3 install -r requirements.txt && celery -A ${PROJECT_NAME} worker -l info"
    working_dir: /app
    volumes:
       - ${APP_PATH}var/www/api.mylaser.fr/mylazer_backend:/app
    environment:
        - PYTHONUNBUFFERED=1
        - MYSQL_HOST=mysql
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - CELERY_BROKER=${CELERY_BROKER}
    depends_on:
        - mysql
        - rabbitmq
beat-worker:
    image: python:3.6
    container_name: beat-worker
    command: bash -c "pip3 install -r requirements.txt && celery -A ${PROJECT_NAME} beat -l info"
    working_dir: /app
    volumes:
       - ${APP_PATH}var/www/api.mylaser.fr/mylazer_backend:/app
    environment:
        - PYTHONUNBUFFERED=1
        - MYSQL_HOST=mysql
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - CELERY_BROKER=${CELERY_BROKER}
    depends_on:
        - mysql
        - rabbitmq