-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
112 lines (83 loc) · 2.73 KB
/
Copy pathDockerfile
File metadata and controls
112 lines (83 loc) · 2.73 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Multi-stage build for production optimization
FROM python:3.11-slim as base
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
DEBIAN_FRONTEND=noninteractive \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
# Install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
gettext \
&& rm -rf /var/lib/apt/lists/*
# Upgrade pip and install poetry
RUN pip install --upgrade pip poetry
# Install Python dependencies
COPY pyproject.toml poetry.lock* ./
RUN poetry config virtualenvs.create false && \
poetry install --no-root --no-interaction --no-ansi --only main
# Production stage
FROM python:3.11-slim as production
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
# Install runtime dependencies only
RUN apt-get update && apt-get install -y \
libpq5 \
gettext \
&& rm -rf /var/lib/apt/lists/*
# Copy Python packages from base stage
COPY --from=base /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=base /usr/local/bin /usr/local/bin
# Create non-root user
RUN groupadd -r django && useradd -r -g django django
# Set work directory
WORKDIR /app
# Copy project files
COPY . .
# Set PYTHONPATH so Python can find dkp module
ENV PYTHONPATH=/app/dkp:$PYTHONPATH
# Create necessary directories
RUN mkdir -p /app/staticfiles /app/media /app/logs && \
chown -R django:django /app
# Collect static files
RUN python dkp/manage.py collectstatic --noinput
# Switch to non-root user
USER django
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python dkp/manage.py check --deploy || exit 1
# Expose port
EXPOSE 8000
# Run Daphne ASGI server
CMD ["daphne", "-b", "0.0.0.0", "-p", "8000", "dkp.asgi:application", "--verbosity", "2"]
# Development stage
FROM python:3.11-slim as development
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
# Install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
gettext \
&& rm -rf /var/lib/apt/lists/*
# Upgrade pip and install poetry
RUN pip install --upgrade pip poetry
# Set work directory
WORKDIR /app
# Install dependencies
COPY pyproject.toml poetry.lock* ./
RUN poetry config virtualenvs.create false && \
poetry install --no-root --no-interaction --no-ansi
# Copy project files
COPY . .
# Create necessary directories
RUN mkdir -p /app/staticfiles /app/media /app/logs
# Keep root user for development (easier debugging)
# Expose port
EXPOSE 8000
# Run Daphne with auto-reload in development
CMD ["daphne", "-b", "0.0.0.0", "-p", "8000", "dkp.asgi:application", "--verbosity", "2"]