Django Docker production solutions

I created python django apps for family use, e. g. for movies and books. I use a local pc for development and a NAS with docker to use in production. The applications are reachable via traefik, but only in the local network, not from internet.

I developed with django version 5.1.6 and used it in production for several month. In production I start - as in development - the application with

python manage.py runserver

In the last days I updated all packages and django to version 5.2.9. The tests in development was successfully. But I get a message, when start the development server:

WARNING: This is a development server. Do not use it in a production setting. Use a production WSGI or ASGI server instead.
For more information on production servers see: https://docs.djangoproject.com/en/5.2/howto/deployment/

So, I updated the application in production and create a new docker image which uses gunicorn. Here is the Dockerfile:

# Stage 1: Base build stage
FROM python:3.13-slim AS builder

# Create the app directory
RUN mkdir /app
 
# Set the working directory
WORKDIR /app
 
# Set environment variables to optimize Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1 
 
# Upgrade pip and install dependencies
RUN pip install --upgrade pip 

# Copy the requirements file first (better caching)
COPY requirements.txt /app/

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Stage 2: Production stage
FROM python:3.13-slim
 
RUN useradd -m -r appuser && \
   mkdir /app && \
   chown -R appuser /app
 
# Copy the Python dependencies from the builder stage
COPY --from=builder /usr/local/lib/python3.13/site-packages/ /usr/local/lib/python3.13/site-packages/
COPY --from=builder /usr/local/bin/ /usr/local/bin/
 
# Set the working directory
WORKDIR /app
 
# Copy application code
COPY --chown=appuser:appuser . .
 
# Set environment variables to optimize Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1 
 
# Switch to non-root user
USER appuser
 
# Expose the application port
EXPOSE 8000 
 
# Start the application using Gunicorn
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "localmovies.wsgi:application"]

In the application localmovies I get data from the TMDB for movies, people and so on. And in production with gunicorn I get serveral errors, e. g.

[CRITICAL] WORKER TIMEOUT (pid:8)

which leads to incomplete data in the database.

I then change the Dockerfile to start the development server and has no issues.

I also try uWSGI, but was not successfull. I see in docker log the message

[uWSGI] getting INI configuration from /uwsgi/uwsgi.ini

and then the container stopped.

Now my questions:

What solution is recommendet for use of a django app in docker,

development-server

gunicorn

uWSGI

...

Have someone a idea, why gunicorn has timeouts and the development server not?

Can someone suggest a howto for uWSGI with django and docker?

Вернуться на верх