Django Docker-Produktionslösungen [Duplikat]Python

Python-Programme
Anonymous
 Django Docker-Produktionslösungen [Duplikat]

Post by Anonymous »

Ich habe Python-Django-Apps für den Familiengebrauch erstellt, z. G. für Filme und Bücher. Ich verwende einen lokalen PC für die Entwicklung und ein NAS mit Docker für die Produktion. Die Anwendungen sind über Traefik erreichbar, jedoch nur im lokalen Netzwerk, nicht über das Internet.
Ich habe mit Django Version 5.1.6 entwickelt und es mehrere Monate lang in der Produktion verwendet. In der Produktion starte ich – wie auch in der Entwicklung – die Anwendung mit

Code: Select all

python manage.py runserver
In den letzten Tagen habe ich alle Pakete und Django auf Version 5.2.9 aktualisiert. Die Tests in der Entwicklung verliefen erfolgreich. Beim Starten des Entwicklungsservers erhalte ich jedoch eine Meldung:

Code: Select all

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/
Also habe ich die Anwendung in der Produktion aktualisiert und ein neues Docker-Image erstellt, das Gunicorn verwendet. Hier ist die Docker-Datei:

Code: Select all

# 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 [url=viewtopic.php?t=25360]environment[/url] 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 [url=viewtopic.php?t=25360]environment[/url] 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 der Anwendung localmovies erhalte ich Daten aus der TMDB für Filme, Personen usw. Und bei der Produktion mit Gunicorn erhalte ich mehrere Fehler, z. B.

Code: Select all

[CRITICAL] WORKER TIMEOUT (pid:8)
was zu unvollständigen Daten in der Datenbank führt.
Ich ändere dann die Docker-Datei, um den Entwicklungsserver zu starten und habe keine Probleme.
Ich habe auch uWSGI ausprobiert, war aber nicht erfolgreich. Ich sehe im Docker-Protokoll die Meldung

Code: Select all

[uWSGI] getting INI configuration from /uwsgi/uwsgi.ini
und dann stoppte der Container.
Jetzt meine Fragen:
Welche Lösung wird für die Verwendung einer Django-App in Docker empfohlen,
Entwicklungsserver
Gunicorn
uWSGI
...
Hat jemand eine Idee, warum Gunicorn das hat? Timeouts und der Entwicklungsserver nicht?
Kann jemand eine Anleitung für uWSGI mit Django und Docker vorschlagen?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post