skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    Okay, so this is what I did that worked for me, so for any new developer like me coming up on the Django web framework.

    The best way I used in deploying the app on Heroku was using the Heroku build manifest. I created a heroku.yml file on the root directory of my project. #This is the Build Manifest for creating web and worker.

    build:
      docker:
        web: Dockerfile
        worker: Dockerfile
    run:
      web: gunicorn my_proj.wsgi:application --bind 0.0.0.0:$PORT
      worker: celery -A my_proj worker -l INFO --beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler
    release:
      image: web
      command:
        - python manage.py collectstatic --noinput
    

    For the Database, I used the Heroku Postgres add-on also for the Redis message broker, I used Heroku Redis.

    I didn't Use Nginx to serve Staticfiles instead I used Whitenoise, pretty easy to set up but nasty bug when you turn off Debug. There are lots of help on here to help you fix that.

    Best you create a static and staticfiles folder in your root directory and in your settings.py file add these, worked for me

    INSTALLED_APP = [
    ..., 
    'whitenoise.runserver_nostatic',
    #BEFORE THE 'django.contrib.staticfiles',
    ]
    STATIC_URL = "/static/"
    STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
    STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]
    WHITENOISE_MANIFEST_STRICT = False
    

    This is how I learned and resolved the issue.

    Any better implementation am always open to learning.


  2. 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
     }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search