-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDockerfile
More file actions
143 lines (109 loc) · 5.05 KB
/
Copy pathDockerfile
File metadata and controls
143 lines (109 loc) · 5.05 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# syntax=docker/dockerfile:1
ARG PYTHON_VERSION=3.12
# --------------------------
# Builder stage: compile dependencies
# --------------------------
FROM python:${PYTHON_VERSION}-slim AS builder
# Install build dependencies first (cached separately from app code).
# Cache-mount apt's package/list dirs so a Dockerfile edit that invalidates
# this layer redownloads nothing already fetched by a prior build. The base
# image's docker-clean config would otherwise wipe the cache right after
# install, so it's removed first (see https://docs.docker.com/build/cache/optimize/#use-cache-mounts).
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
rm -f /etc/apt/apt.conf.d/docker-clean \
&& apt-get update && apt-get install -y --no-install-recommends \
libpq-dev \
gdal-bin \
libgdal-dev \
build-essential \
gcc
ENV GDAL_LIBRARY_PATH=/usr/lib/libgdal.so
# Copy ONLY requirements.txt (not app code) so pip install layer caches independently
COPY requirements.txt /tmp/requirements.txt
# Use BuildKit cache mount for pip (persistent across builds)
RUN --mount=type=cache,target=/root/.cache/pip \
python -m pip install --user --no-warn-script-location --no-compile --root-user-action=ignore \
-r /tmp/requirements.txt
# --------------------------
# Runtime base stage: minimal production runtime
# --------------------------
FROM python:${PYTHON_VERSION}-slim AS runtime-base
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV GDAL_LIBRARY_PATH=/usr/lib/libgdal.so
ENV PATH="/usr/local/bin:$PATH"
# Install runtime-only system dependencies BEFORE app code (better cache).
# GeoDjango only needs the GDAL shared library via ctypes (GDAL_LIBRARY_PATH) —
# the versioned libgdalNN runtime package provides that without pulling in
# gdal-bin's CLI toolchain or libgdal-dev's headers. The NN suffix tracks the
# Debian release's GDAL SONAME (e.g. libgdal32 on bookworm, libgdal36 on
# trixie) and shifts whenever the upstream base image does, so resolve it
# dynamically rather than hardcoding it. It also only ships a versioned
# filename (e.g. libgdal.so.32), so symlink the unversioned name
# GDAL_LIBRARY_PATH expects.
# Cache-mounted the same way as the builder stage's apt install above.
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
rm -f /etc/apt/apt.conf.d/docker-clean \
&& apt-get update \
&& GDAL_PKG="$(apt-cache search --names-only '^libgdal[0-9]+$' | cut -d' ' -f1)" \
&& apt-get install -y --no-install-recommends \
libpq5 \
"$GDAL_PKG" \
&& ln -s "$(find /usr/lib -name 'libgdal.so.*' | sort -V | tail -1)" /usr/lib/libgdal.so
WORKDIR /app
# Create app user BEFORE copying code (avoid invalidating on code changes)
ARG UID=1000
RUN adduser \
--disabled-password \
--gecos "" \
--home "/home/appuser" \
--shell "/bin/bash" \
--uid "${UID}" \
appuser \
&& mkdir -p /home/appuser \
&& chown -R appuser:appuser /home/appuser
# Copy pre-built Python packages from builder stage
COPY --from=builder --chown=appuser:appuser /root/.local /home/appuser/.local
ENV PATH="/home/appuser/.local/bin:$PATH"
# Copy application files (changes frequently, so last in layer chain)
COPY --chown=appuser:appuser . .
COPY --chown=appuser:appuser --chmod=755 entrypoint.sh /app/entrypoint.sh
RUN mkdir -p /app/staticfiles /app/media && chown appuser:appuser /app/staticfiles /app/media
USER appuser
ENTRYPOINT ["/app/entrypoint.sh"]
# --------------------------
# Celery worker service
# --------------------------
FROM runtime-base AS celery
CMD ["celery", "-A", "progress_rpg", "worker", "--loglevel=info"]
# --------------------------
# Celery Beat scheduler service
# --------------------------
FROM runtime-base AS celery-beat
CMD ["celery", "-A", "progress_rpg", "beat", "--loglevel=info", "--scheduler", "django_celery_beat.schedulers:DatabaseScheduler"]
# --------------------------
# Web service (local dev): same runtime as web, minus collectstatic.
# compose.yaml bind-mounts the repo over /app for the web service, so the
# baked-in COPY and collected static files below are shadowed anyway —
# running collectstatic here would just be wasted build time.
# --------------------------
FROM runtime-base AS dev
EXPOSE 8000
ENV PORT=8000
ENV DJANGO_SETTINGS_MODULE=progress_rpg.settings.dev
CMD ["daphne", "-b", "0.0.0.0", "-p", "8000", \
"--ping-interval", "20", "--ping-timeout", "30", \
"progress_rpg.asgi:application"]
# --------------------------
# Web service: Django ASGI server (must be last — Render builds final stage by default)
# --------------------------
FROM runtime-base AS web
EXPOSE 8000
ENV PORT=8000
ENV DJANGO_SETTINGS_MODULE=progress_rpg.settings.prod
RUN SECRET_KEY=dummy DATABASE_URL=postgres://dummy:dummy@localhost/dummy python manage.py collectstatic --noinput --clear
CMD ["daphne", "-b", "0.0.0.0", "-p", "8000", \
"--ping-interval", "20", "--ping-timeout", "30", \
"progress_rpg.asgi:application"]