简体   繁体   中英

How to run Python Django and Celery using docker-compose?

I've a Python application using Django and Celery, and I trying to run using docker and docker-compose because i also using Redis and Dynamodb

The problem is the following:

I'm not able to execute both services WSGI and Celery , cause just the first instruction works fine..

version: '3.3'

services:
  redis:
    image: redis:3.2-alpine
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
  dynamodb:
    image: dwmkerr/dynamodb
    ports:
      - "3000:8000"
    volumes:
      - dynamodb_data:/data
  jobs:
    build:
      context: nubo-async-cfe-seces
      dockerfile: Dockerfile
    environment:
      - REDIS_HOST=redisrvi
      - PYTHONUNBUFFERED=0
      - CC_DYNAMODB_NAMESPACE=None
      - CC_DYNAMODB_ACCESS_KEY_ID=anything
      - CC_DYNAMODB_SECRET_ACCESS_KEY=anything
      - CC_DYNAMODB_HOST=dynamodb
      - CC_DYNAMODB_PORT=8000
      - CC_DYNAMODB_IS_SECURE=False

    command: >
      bash -c "celery worker -A tasks.async_service -Q dynamo-queue -E --loglevel=ERROR &&
               uwsgi --socket 0.0.0.0:8080 --protocol=http --wsgi-file nubo_async/wsgi.py"
    depends_on:
      - redis
      - dynamodb
    volumes:
      - .:/jobs
    ports:
      - "9090:8080"
volumes:
  redis_data:
  dynamodb_data:

Has anyone had the same problem?

Here's the docker-compose as suggested by @Satevg , run the Django and Celery application by separate containers. Works fine!

version: '3.3'

services:
  redis:
    image: redis:3.2-alpine
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
  dynamodb:
    image: dwmkerr/dynamodb
    ports:
      - "3000:8000"
    volumes:
      - dynamodb_data:/data
  jobs:
    build:
      context: nubo-async-cfe-services
      dockerfile: Dockerfile
    environment:
      - REDIS_HOST=redis
      - PYTHONUNBUFFERED=0
      - CC_DYNAMODB_NAMESPACE=None
      - CC_DYNAMODB_ACCESS_KEY_ID=anything
      - CC_DYNAMODB_SECRET_ACCESS_KEY=anything
      - CC_DYNAMODB_HOST=dynamodb
      - CC_DYNAMODB_PORT=8000
      - CC_DYNAMODB_IS_SECURE=False    
    command: bash -c "uwsgi --socket 0.0.0.0:8080 --protocol=http --wsgi-file nubo_async/wsgi.py"
    depends_on:
      - redis
      - dynamodb
    volumes:
      - .:/jobs
    ports:
      - "9090:8080"
  celery:
    build:
      context: nubo-async-cfe-services
      dockerfile: Dockerfile
    environment:
    - REDIS_HOST=redis
    - PYTHONUNBUFFERED=0
    - CC_DYNAMODB_NAMESPACE=None
    - CC_DYNAMODB_ACCESS_KEY_ID=anything
    - CC_DYNAMODB_SECRET_ACCESS_KEY=anything
    - CC_DYNAMODB_HOST=dynamodb
    - CC_DYNAMODB_PORT=8000
    - CC_DYNAMODB_IS_SECURE=False
    command: celery worker -A tasks.async_service -Q dynamo-queue -E --loglevel=ERROR
    depends_on:
    - redis
    - dynamodb
    volumes:
    - .:/jobs
volumes:
  redis_data:
  dynamodb_data:

You may refer to docker-compose of Saleor project. I would suggest to let celery run its daemon only depend on redis as the broker. See the configuration of docker-compose.yml file:

services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
      args:
        STATIC_URL: '/static/'
    restart: unless-stopped
    networks:
      - saleor-backend-tier
    env_file: common.env
    depends_on:
      - db
      - redis

    celery:
        build:
          context: .
          dockerfile: ./Dockerfile
          args:
            STATIC_URL: '/static/'
        command: celery -A saleor worker --app=saleor.celeryconf:app --loglevel=info
        restart: unless-stopped
        networks:
          - saleor-backend-tier
        env_file: common.env
        depends_on:
          - redis

See also that the connection from both services to redis are set separately by the environtment vatables as shown on the common.env file:

CACHE_URL=redis://redis:6379/0
CELERY_BROKER_URL=redis://redis:6379/1

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