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.