-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDockerfile.prod
More file actions
79 lines (61 loc) · 1.93 KB
/
Dockerfile.prod
File metadata and controls
79 lines (61 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Multi-stage build for production
FROM python:3.11-slim as builder
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
# Install system dependencies required for building
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
python3-dev \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Create and use non-root user
RUN groupadd -r django && useradd -r -g django django
# Set work directory
WORKDIR /app
# Copy requirements and install Python dependencies
COPY requirements.txt /app/
RUN pip install --user --no-cache-dir -r requirements.txt
# Production stage
FROM python:3.11-slim as production
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PATH="/home/django/.local/bin:$PATH"
# Install only runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
libmagic1 \
libgl1-mesa-glx \
libglib2.0-0 \
libsm6 \
libxext6 \
libxrender-dev \
libgomp1 \
netcat-openbsd \
curl \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# Create and use non-root user
RUN groupadd -r django && useradd -r -g django django
# Set work directory
WORKDIR /app
# Copy Python dependencies from builder stage
COPY --from=builder --chown=django:django /root/.local /home/django/.local
# Copy application code
COPY --chown=django:django . /app/
# Make entrypoint scripts executable
RUN chmod +x /app/bin/*.sh
# Create directories for static files and media
RUN mkdir -p /app/staticfiles /app/media && \
chown -R django:django /app/staticfiles /app/media
# Switch to non-root user
USER django
# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/api/health/ || exit 1
EXPOSE 8000
# Default command
CMD ["/app/bin/production_entrypoint.sh"]