Search code examples
djangodockerherokucelerycelerybeat

I am trying to deploy Deploy a Django with Celery, Celery-beat, Redis, Postgresql, Nginx, Gunicorn with Docker to Heroku


I am trying to deploy my app to Heroku, am using amazon s3 bucket for static but the static icon is not showing on the website, and I need help setting up a worker for celery and celery beat to work on Heroku.

This is my Dockerfile:

# pull the official base image
FROM python:3.8.3-alpine as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
    && apk add postgresql-dev gcc python3-dev musl-dev
RUN apk add zlib libjpeg-turbo-dev libpng-dev \
    freetype-dev lcms2-dev libwebp-dev \
    harfbuzz-dev fribidi-dev tcl-dev tk-dev 
# lint
RUN pip install --upgrade pip
RUN pip install flake8
COPY . .

# install dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt

# pull official base image
FROM python:3.8.3-alpine

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup -S app && adduser -S app -G app

# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/static
RUN mkdir $APP_HOME/media
WORKDIR $APP_HOME


# install dependencies
RUN apk update && apk add libpq 
RUN apk add zlib libjpeg-turbo-dev libpng-dev \
    freetype-dev lcms2-dev libwebp-dev \
    harfbuzz-dev fribidi-dev tcl-dev tk-dev 
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --no-cache /wheels/*

# copy entrypoint.sh
COPY ./entrypoint.sh $APP_HOME

# copy project
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]

CMD gunicorn my_proj.wsgi:application --bind 0.0.0.0:$PORT ```

This is my docker-compose file 

```version: "3.8"

services:
    web:
        build: 
            context: .
            dockerfile : Dockerfile
        container_name: django
        command: gunicorn my_proj.wsgi:application --bind 0.0.0.0:8000
        volumes:
            - static_volume:/home/app/web/static
            - media_volume:/home/app/web/media
        expose:
            - 8000
        depends_on:
            - pgdb
            - redis
    celery-worker:
        build: .
        command: celery -A my_proj worker -l INFO
        volumes:
            - .:/usr/src/app
        environment:
            - DEBUG=1
            - DJANGO_ALLOWED_HOSTS=['localhost', '127.0.0.1', 'app_name.herokuapp.com']
            - CELERY_BROKER=redis://redis:6379/0
            - CELERY_BACKEND=redis://redis:6379/0
        depends_on:
            - web
            - redis
    celery-beat:
        build: .
        command: celery -A my_proj beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler
        volumes:
            - .:/usr/src/app
        environment:
            - DEBUG=1
            - DJANGO_ALLOWED_HOSTS=['localhost', '127.0.0.1', 'app_name.herokuapp.com']
            - CELERY_BROKER=redis://redis:6379/0
            - CELERY_BACKEND=redis://redis:6379/0
        depends_on:
            - web
            - redis
            - celery-worker
    pgdb:
        image: postgres
        container_name: pgdb
        environment:
            - POSTGRES_DB=databasename
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=password
        volumes:
            - pgdata:/var/lib/postgresql/data/
    redis:
        image: "redis:alpine"
    nginx:
        build: ./nginx
        volumes:
            - static_volume:/home/app/web/static
            - media_volume:/home/app/web/media
        ports:
            - 1337:80
        depends_on:
            - web
volumes:
    pgdata:
    static_volume:
    media_volume: ```


Here are the steps I followed to deploy on Heroku

heroku container:login
docker build -t registry.heroku.com/app name/web .
docker push registry.heroku.com/app name/web
heroku container:release -a app name web

Also, I ran this below and it is starting but I want it to work on its own.

heroku run celery -A my_proj worker -l INFO -a <app name>
heroku run celery -A my_proj beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler -a <app name>

Issue:

  1. How do I set up celery to work as a worker.
  2. How do I also set up a worker for celery-beat

Please I need help, in development, I usually use docker-compose up -d --build, and all images are built and working together. I feel it's only the Django container that is just working, and others were not built.


Solution

  • Try setting up CORS in AWS S3 bucket permission to get the fonts working.

    [
     {
        "AllowedHeaders": [
            "Authorization"
        ],
        "AllowedMethods": [
            "GET",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
     }
    ]