skip to Main Content

When I start docker container with docker compose up command for some reason I get this error:

celery-worker               | Usage: celery [OPTIONS] COMMAND [ARGS]...
celery-worker               | Try 'celery --help' for help.
celery-worker               | 
celery-worker               | Error: Invalid value for '-A' / '--app': 
celery-worker               | Unable to load celery application.
celery-worker               | While trying to load the module backend.celery_app.app the following error occurred:
celery-worker               | Traceback (most recent call last):
celery-worker               |   File "/usr/local/lib/python3.11/site-packages/celery/bin/celery.py", line 58, in convert
celery-worker               |     return find_app(value)
celery-worker               |            ^^^^^^^^^^^^^^^
celery-worker               |   File "/usr/local/lib/python3.11/site-packages/celery/app/utils.py", line 383, in find_app
celery-worker               |     sym = symbol_by_name(app, imp=imp)
celery-worker               |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
celery-worker               |   File "/usr/local/lib/python3.11/site-packages/kombu/utils/imports.py", line 59, in symbol_by_name
celery-worker               |     module = imp(module_name, package=package, **kwargs)
celery-worker               |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
celery-worker               |   File "/usr/local/lib/python3.11/site-packages/celery/utils/imports.py", line 104, in import_from_cwd
celery-worker               |     return imp(module, package=package)
celery-worker               |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
celery-worker               |   File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
celery-worker               |     return _bootstrap._gcd_import(name[level:], package, level)
celery-worker               |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
celery-worker               |   File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
celery-worker               |   File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
celery-worker               |   File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
celery-worker               |   File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
celery-worker               |   File "<frozen importlib._bootstrap_external>", line 940, in exec_module
celery-worker               |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
celery-worker               |   File "/backend/backend/celery_app.py", line 10, in <module>
celery-worker               |     app.conf.broker_url = settings.CELERY_BROKER_URL
celery-worker               |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^
celery-worker               |   File "/usr/local/lib/python3.11/site-packages/django/conf/__init__.py", line 102, in __getattr__
celery-worker               |     self._setup(name)
celery-worker               |   File "/usr/local/lib/python3.11/site-packages/django/conf/__init__.py", line 89, in _setup
celery-worker               |     self._wrapped = Settings(settings_module)
celery-worker               |                     ^^^^^^^^^^^^^^^^^^^^^^^^^
celery-worker               |   File "/usr/local/lib/python3.11/site-packages/django/conf/__init__.py", line 217, in __init__
celery-worker               |     mod = importlib.import_module(self.SETTINGS_MODULE)
celery-worker               |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
celery-worker               |   File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
celery-worker               |     return _bootstrap._gcd_import(name[level:], package, level)
celery-worker               |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
celery-worker               |   File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
celery-worker               |   File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
celery-worker               |   File "<frozen importlib._bootstrap>", line 1128, in _find_and_load_unlocked
celery-worker               |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
celery-worker               |   File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
celery-worker               |   File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
celery-worker               |   File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
celery-worker               | ModuleNotFoundError: No module named 'core'
celery-worker               | 
celery-worker exited with code 2

Here is my porject structure:

web-for-cyclers/
  backend/
    core/
      __init.py__
      asgi.py
      settings.py
      urls.py
      wsgi.py
     
    static
    venv
    Dockerfile
    manage.py
    requirements.txt
    web.env
    .dockerignore
    .gitignore
    celery_app.py

 
  frontend/
    node_modules/
    public/
    src/
    .dockerignore
    Dockerfile
    package.json
    package-lock.json
    .gitignore
    

  nginx/
    Dockerfile
    nginx.conf
  docker-compose.yml

celery_app.py file:

import os

from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

app = Celery('backend.core')
app.config_from_object('django.conf:settings')
app.conf.broker_url = settings.CELERY_BROKER_URL
app.autodiscover_tasks()

in core.settings I have a line for broker:

CELERY_BROKER_URL = 'redis://redis:6379/0'

init.py in core folder, where the settings.py file is:

from celery_app import app as celery_app

__all__ = ('celery_app',)

and finally docker-compose.yml:

services:

  # PostgreSQL
  db:
    image: postgres:14.1-alpine
    restart: always
    env_file:
      - backend/web.env
    ports:
      - '5432:5432'
    volumes:
      - db:/var/lib/postgresql/data

  # Backend Django
  backend:
    build:
      context: ./backend
    command: bash -c "./manage.py collectstatic --noinput && ./manage.py migrate && gunicorn -b 0.0.0.0:8000 core.wsgi:application —log-level debug"
    ports:
      - '8000:8000'
    depends_on:
      - db
    env_file:
      - backend/web.env
    volumes:
      - ./backend/:/backend
      - static_volume:/backend/static
      - media_volume:/backend/media

  # Frontend Next.js
  frontend:
    build:
      context: ./frontend
    command: npm run dev
    depends_on:
      - backend
    volumes:
      - .:/app/frontend
      - /app/node_modules
      - /app/.next
    ports:
      - '3000:3000'

  # Nginx
  nginx:
    build:
      dockerfile: ./Dockerfile
      context: ./nginx
    ports:
      - '80:80'
    volumes:
      - static_volume:/backend/static
      - media_volume:/backend/media
    depends_on:
      - backend
      - frontend
      - db

      # Redis
      redis:
        image: redis:7.0.5-alpine
        container_name: redis
    
      # Celery Worker 1
      celery-worker:
        restart: always
        build:
          context: ./backend
        entrypoint: celery
        command: -A backend.celery_app.app worker --loglevel=info
        volumes:
          - .:/backend
        container_name: celery-worker
        depends_on:
          - db
          - redis
          - backend
        links:
          - redis
        env_file:
          - backend/web.env
    
      # Flower
      flower:
        build:
          context: ./backend
        container_name: flower
        command: celery -A backend.celery_app.app flower
        links:
          - redis
        depends_on:
          - redis
          - backend
          - db
        ports:
          - '5555:5555'
        volumes:
          - .:/backend
    
    
    volumes:
      db:
        driver: local
      react_build:
      static_volume:
        driver: local
      media_volume:
        driver: local

I would really appreciate if someone helps. Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    This docker compose configuration for celery worked for me:

      celery-worker:
        restart: always
        build:
          context: ./backend
        entrypoint: /bin/sh
        command: -c "export PYTHONPATH=/backend:/backend/backend && celery -A backend.celery_app:app worker --loglevel=info"
        volumes:
          - .:/backend
        container_name: celery-worker
        depends_on:
          - db
          - redis
          - backend
        links:
          - redis
        env_file:
          - backend/web.env
    

  2. Have you tried

    os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘backend.core.settings’)

    in celery_app.py ?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search