2

when I am trying to run my application I using without docker its working perfectly , but In docker-compose I am getting this error :

               | Error: Invalid value for '-A' / '--app': 
               | Unable to load celery application.
               | The module sampleproject was not found.

my docker-compose file

  app:
    container_name: myapp
    hostname: myapp
    build:
      context: .
      dockerfile: Dockerfile
    image: sampleproject
    tty: true
    command: >
      bash -c "
        python manage.py migrate &&
        python manage.py runserver 0.0.0.0:8000
      "
    env_file: .env
    ports:
      - "8000:8000"
    volumes:
      - .:/project
    depends_on:
      - database
      - redis

  redis:
    image: redis:alpine

  celery:
    build:
      context: ./
      dockerfile: Dockerfile
    command: celery  -A sampleproject worker -l info
    depends_on:
     - database
     - redis

  celery-beat:
    build: .
    command: celery -A sampleproject beat -l info
    depends_on:
      - database
      - redis
      - celery

my Docker file

FROM python:3.8
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt --no-cache-dir \
  && rm -rf requirements.txt

RUN mkdir /project
WORKDIR /project

my folder structure is something like this :

enter image description here

tempaccount as
  • 173
  • 1
  • 2
  • 10
  • from your `Dockerfile` it seems like you don't add your app source code (unless it's installed as a package in your `requirements.txt` which I cannot see here – ItayB Jan 01 '21 at 16:37
  • @ItayB which app sir ? are you talking about docker-compose `app` ? – tempaccount as Jan 01 '21 at 16:41
  • `app = Celery('my_app', broker='amqp://guest@localhost//')` – ItayB Jan 01 '21 at 16:52
  • where I am suppose to put this ? I have this in `celery.py` as `os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sampleproject.settings") app = Celery("sampleproject") ` @ItayB – tempaccount as Jan 01 '21 at 16:56
  • Try to inspect the `celery` container that is running after compose is up to check if there is actually a `sampleproject` folder. Make sure the context on which the celery command is executed, it's the expected one, sampleproject should be a sub-folder. – NicoE Jan 01 '21 at 22:12
  • @NicoE I dont know how I can inspect into celery container . is there any specific command ? – tempaccount as Jan 02 '21 at 15:19
  • I meant something like `docker-compose exec celery bash`, being *celery* the name of the service declared on your compose file. – NicoE Jan 02 '21 at 20:24

1 Answers1

2

I had the same problem and with that we found the solution. In fact, celery is right to complain about not being able to run, as it needs an instance of the application.

For that, it is only necessary to add the volume directive in the docker-compose .yaml file, directing to the project folder, in case into the service celery and celery-beat.

Example:

   app:
    container_name: myapp
    hostname: myapp
    build:
      context: .
      dockerfile: Dockerfile
    image: sampleproject
    tty: true
    command: >
      bash -c "
        python manage.py migrate &&
        python manage.py runserver 0.0.0.0:8000
      "
    env_file: .env
    ports:
      - "8000:8000"
    volumes:
      - .:/project
    depends_on:
      - database
      - redis

  redis:
    image: redis:alpine

  celery:
    build:
      context: ./
      dockerfile: Dockerfile
    command: celery  -A sampleproject worker -l info
    volumes:
      - .:/project
    depends_on:
     - database
     - redis

  celery-beat:
    build: .
    command: celery -A sampleproject beat -l info
    volumes:
      - .:/project
    depends_on:
      - database
      - redis
      - celery

So when the celery container is executed it will see that there is a volume and will execute the project without any problems.

vic.py
  • 409
  • 10
  • 22