Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d4307bdaa |
40
Dockerfile
40
Dockerfile
@@ -1,78 +1,62 @@
|
|||||||
# Multi-stage Dockerfile for Django application
|
# Multi-stage Dockerfile for Django backend (zonco site API)
|
||||||
# Stage 1: Builder stage - Install dependencies
|
# - funzone-site/docker-compose.yml: dev (runserver + bind mount)
|
||||||
FROM python:3.11-slim as builder
|
# - site_backend/docker-compose.yml: prod (gunicorn)
|
||||||
|
|
||||||
|
FROM python:3.11-slim AS builder
|
||||||
|
|
||||||
# Set environment variables
|
|
||||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
PYTHONUNBUFFERED=1 \
|
PYTHONUNBUFFERED=1 \
|
||||||
PIP_NO_CACHE_DIR=1 \
|
PIP_NO_CACHE_DIR=1 \
|
||||||
PIP_DISABLE_PIP_VERSION_CHECK=1
|
PIP_DISABLE_PIP_VERSION_CHECK=1
|
||||||
|
|
||||||
# Install system dependencies required for building Python packages
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
gcc \
|
gcc \
|
||||||
postgresql-client \
|
postgresql-client \
|
||||||
libpq-dev \
|
libpq-dev \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Create and set working directory
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy requirements file
|
|
||||||
COPY requirements.txt .
|
COPY requirements.txt .
|
||||||
|
|
||||||
# Install Python dependencies
|
|
||||||
RUN pip install --upgrade pip && \
|
RUN pip install --upgrade pip && \
|
||||||
pip install --user -r requirements.txt
|
pip install --user -r requirements.txt
|
||||||
|
|
||||||
# Stage 2: Production stage - Minimal runtime image
|
|
||||||
FROM python:3.11-slim
|
FROM python:3.11-slim
|
||||||
|
|
||||||
# Set environment variables
|
|
||||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
PYTHONUNBUFFERED=1 \
|
PYTHONUNBUFFERED=1 \
|
||||||
PATH="/home/django/.local/bin:$PATH"
|
PATH="/home/django/.local/bin:$PATH" \
|
||||||
|
POSTGRES_HOST=db \
|
||||||
|
POSTGRES_PORT=5432 \
|
||||||
|
ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0,panel.zoneco.org
|
||||||
|
|
||||||
# Install runtime dependencies only
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
postgresql-client \
|
postgresql-client \
|
||||||
libpq5 \
|
libpq5 \
|
||||||
bash \
|
bash \
|
||||||
|
curl \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Create non-root user for security
|
|
||||||
RUN groupadd -r django && useradd -r -g django django
|
RUN groupadd -r django && useradd -r -g django django
|
||||||
|
|
||||||
# Create necessary directories
|
|
||||||
RUN mkdir -p /app/staticfiles /app/media && \
|
RUN mkdir -p /app/staticfiles /app/media && \
|
||||||
chown -R django:django /app
|
chown -R django:django /app
|
||||||
|
|
||||||
# Set working directory
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy Python dependencies from builder stage
|
|
||||||
COPY --from=builder /root/.local /home/django/.local
|
COPY --from=builder /root/.local /home/django/.local
|
||||||
|
RUN chown -R django:django /home/django/.local
|
||||||
|
|
||||||
# Copy project files
|
|
||||||
COPY --chown=django:django . .
|
COPY --chown=django:django . .
|
||||||
|
|
||||||
# Copy and set permissions for entrypoint script
|
|
||||||
COPY --chown=django:django entrypoint.sh /app/entrypoint.sh
|
COPY --chown=django:django entrypoint.sh /app/entrypoint.sh
|
||||||
RUN chmod +x /app/entrypoint.sh
|
RUN chmod +x /app/entrypoint.sh
|
||||||
|
|
||||||
# Switch to non-root user
|
|
||||||
USER django
|
USER django
|
||||||
|
|
||||||
# Expose port
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
# Health check
|
|
||||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
||||||
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/')" || exit 1
|
CMD curl -f http://localhost:8000/api/ || exit 1
|
||||||
|
|
||||||
# Use entrypoint script
|
|
||||||
ENTRYPOINT ["/app/entrypoint.sh"]
|
ENTRYPOINT ["/app/entrypoint.sh"]
|
||||||
|
|
||||||
# Default command (can be overridden in docker-compose)
|
|
||||||
CMD ["gunicorn", "zonco_backend.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3", "--timeout", "120"]
|
CMD ["gunicorn", "zonco_backend.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3", "--timeout", "120"]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user