Dockerized Django with Gunicorn :How to check if it's working ?

I’m attemtping to migrate to Gunicorn WSGI server for a Dockerized Django 5.0.4 Rest API running on 80:8080. Currently it is running with the default WSGI that ships with Django. However, I’m unable to set it up correctly. Definately missing something.Also, guncicorn is installed using

pip install -r requirements.txt --quiet

part of entrypoint.sh

Postman response still shows response as:

Server : WSGIServer/0.2 CPython/3.10.14

Here is my configuration:

Project structure

mydjangoblogmain 
    ---mydjangoblog
        -settings.py
        -wsgi.py
        -urls.py

wsgi.py inside mydjangoblog

"""
WSGI config for mydjangoblogmain project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mydjangoblog.settings")

application = get_wsgi_application()

My Docker File

FROM python:3.10.14

RUN apt-get -y update && apt-get -y upgrade &&  apt-get install -y ffmpeg

COPY wait-for-it.sh /wait-for-it.sh

# Copy any files over
COPY entrypoint.sh /entrypoint.sh

# Copy any files over
COPY bootstrap_development_data.sh /bootstrap_development_data.sh

#Change working directory to create gunicorn log files
WORKDIR /opt
RUN mkdir -p .gunicorn-logs
RUN touch -p /opt/.gunicorn-logs/error.log
RUN touch -p /opt/.gunicorn-logs/access.log
RUN chmod +x /opt/.gunicorn-logs/

# Change working directory again. 
WORKDIR /opt/mydjangoblogmain

# Change permissions
RUN chmod +x /entrypoint.sh
RUN chmod +x /bootstrap_development_data.sh
RUN chmod +x /wait-for-it.sh

ENTRYPOINT ["/entrypoint.sh"]

COPY requirements.txt /requirements.txt

RUN pip install -r /requirements.txt

VOLUME ["/opt/mydjangoblogmain"]

EXPOSE 8080

CMD ["gunicorn","--config=gunicorn_config.py","kosmosnetwork.wsgi:application"]

# CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"]  #Commented this out and used the CMD command before it.

entrypoint.sh

#!/bin/bash
set -eo pipefail

cd /opt/mydjangoblogmain

# install pip env deps, run migrations, collect media, start the server
pip install -r requirements.txt --quiet

python manage.py migrate
echo "yes" | python manage.py collectmedia

exec $@

docker-compose.yml

api-webserver:
    container_name: mydjangoblogmain-api-webserver
    build:
      dockerfile: Dockerfile
      context: ./.docker/api
    privileged: True
    extra_hosts:
      - db.blog:172.16.16.4
    volumes:
      - ./:/opt/mydjangoblogmain # API
      - ./.docker-cache/pip:/root/.cache/pip # Package Cache
    ports:
        - "80:8080" # Host Blog API port 80 mapped to internal API port 8080
    working_dir: /opt/mydjangoblogmain
    networks:
      blog:
        ipv4_address: 172.16.16.9
    depends_on:
      - db
      - redis
    env_file:
      - .docker-compose.env 
      
  

I also tried creating a separate gunicorn_config.py in the root directory mydjangoblogmain:

bind = “0.0.0.0:8080”

loglevel = “INFO”

workers = “4”

threads=“10”

reload = True

errorlog = “/opt/.gunicorn-logs/error.log”

accesslog = “/opt/.gunicorn-logs/access.log”

And in Dockerfile:

CMD [“gunicorn”,“–config=gunicorn_config.py”,“mydjangoblogmain.mydjangoblog.wsgi:application”]

replacing the exisiting CMD command. But yet not working.

Help appreciated.

If /opt/mydjangoblogmain is your project directory, and your current directory when starting gunicorn, then

should actually be:
"mydjangoblog.wsgi:application"

Module references are relative to your current directory.

(This is assuming that the full structure is /opt/mydjangoblogmain/mydjangoblog and not /opt/mydjangoblogmain/mydjangoblogmain/mydjangoblog)

If this is not the issue, then check your docker logs and your gunicorn logs for errors.

Understood.

Also, gunicorn_config.py is placed inside the root directory mydjangoblogmain.

Hello. Not working. Here is my revised dockerFile after attempting to create the log directories. The log directories are not being created somehow.

Also, guncicorn is installed using

pip install -r requirements.txt --quiet

part of entrypoint.sh

Revised dockerFile

FROM python:3.10.14

RUN apt-get -y update && apt-get -y upgrade && apt-get install -y ffmpeg

COPY wait-for-it.sh /wait-for-it.sh

Copy any files over

COPY entrypoint.sh /entrypoint.sh

Copy any files over

COPY bootstrap_development_data.sh /bootstrap_development_data.sh

#Change working directory to create gunicorn log files
WORKDIR /opt
RUN mkdir -p .gunicorn-logs
RUN touch -p /opt/.gunicorn-logs/error.log
RUN touch -p /opt/.gunicorn-logs/access.log
RUN chmod +x /opt/.gunicorn-logs/

Change working directory again.

WORKDIR /opt/mydjangoblogmain

Change permissions

RUN chmod +x /entrypoint.sh
RUN chmod +x /bootstrap_development_data.sh
RUN chmod +x /wait-for-it.sh

ENTRYPOINT [“/entrypoint.sh”]

COPY requirements.txt /requirements.txt

RUN pip install -r /requirements.txt

VOLUME [“/opt/mydjangoblogmain”]

EXPOSE 8080

CMD [“gunicorn”,“–config=gunicorn_config.py”,“kosmosnetwork.wsgi:application”]

Where are you looking for them? From what I can see of what you have posted here, they would be created inside the container and not mapped to the host.

What does “not working” mean? Do you have an error message? If so, copy paste it in full.

Yes that is correct. I entered the container and the logs directory isn’ t being created inside the container. it should be inside the /opt/ directory but it isnt there. Thats’ why I wanted to know iI need to create those directories (which I did in the revised dockerFile, unsuccessful though).

Hello. Not working means, I’m getting a Postman API response which is still showing

WSGIServer/0.2 CPython/3.10.14 Value in the “Server” key.

Should’nt Gunicorn be returned here ?

How do I check if its working insider the Container ?

What do your docker logs for the container show?

Only operations related to Migrations are listed there. Operations to perform, Apply migrations…and Run migrations.

Is this a custom management command you have written? This isn’t a standard command - what does it do?

If this isn’t showing up in the docker logs, then it appears to me that this command may be hanging or failing for some reason.

Yup. It does show up in the docker logs.

I’m attempting to deploy the following repo with a gunicorn server.

I have changed the root folder name okuna-api to mydjangoblogmain and openbook to mydjangoblog just to give the readers a simpler version of the problem at hand.

Here is the complete log.

Operations to perform:
Apply all migrations: admin, auth, authtoken, contenttypes, django_rq, openbook_auth, openbook_categories, openbook_circles, openbook_common, openbook_communities, openbook_connections, openbook_devices, openbook_follows, openbook_hashtags, openbook_invitations, openbook_lists, openbook_moderation, openbook_notifications, openbook_posts, scheduler, sessions, video_encoding
Running migrations:
No migrations to apply.
Your models in app(s): ‘scheduler’ have changes that are not yet reflected in a migration, and so won’t be applied.
Run ‘manage.py makemigrations’ to make new migrations, and then re-run ‘manage.py migrate’ to apply them.

You have requested to collect media files at the destination
location as specified in your settings:

/opt/okuna-api/media

This will overwrite existing files!
Are you sure you want to do this?

Type ‘yes’ to continue, or ‘no’ to cancel:
0 media files copied to ‘/opt/okuna-api/media’, 67 unmodified.
Installed 392 object(s) from 6 fixture(s)

What is “it” here? It’s hard to understand what you are asking…

Apologies. Its the .gunicorn-logs directory which I’m attempting to create whcih isnt getting created inside the container in my attempt to track whats wrong.

These are the 4 lines which I added lately for the same part of the dockerFile:

#Change working directory to create gunicorn log files
WORKDIR /opt
RUN mkdir -p .gunicorn-logs
RUN touch -p /opt/.gunicorn-logs/error.log
RUN touch -p /opt/.gunicorn-logs/access.log
RUN chmod +x /opt/kosmos-api-core/.gunicorn-logs/