Django 5.2
[wolnelektury.git] / Dockerfile
1 # Wolne Lektury images.
2 #
3 # Stages marked with ##### are build targets:
4 #   deps      dev image (docker-compose.yml, code mounted)
5 #   test      docker-compose.test.yml
6 #   prod      production app image (default, last stage)
7 #   nginx     production nginx with static files
8 #   postgres  Postgres with Polish dictionary
9 # Stages marked with ===== are intermediate.
10
11
12 # Global build arguments. Stages using UID/GID must redeclare them
13 # (bare `ARG UID`) to bring them into scope; the defaults are set here.
14
15 ARG PYTHON_IMAGE=python:3.12-trixie
16 ARG UID=8100
17 ARG GID=8100
18
19
20 ##########
21 #  deps  #############################################################
22 #
23 #  Base system and requirements.
24 #  Used directly as the dev image, with code mounted.
25 #
26 FROM ${PYTHON_IMAGE} AS deps
27
28 ARG UID
29 ARG GID
30
31 RUN     apt-get update && apt-get install -y \
32         git \
33         calibre \
34         texlive-xetex texlive-lang-polish \
35         texlive-extra-utils \
36         texlive-lang-greek \
37         texlive-lang-other \
38         texlive-luatex \
39         texlive-fonts-extra \
40         texlive-fonts-extra-links \
41         fonts-noto-core fonts-noto-extra
42
43 # Install dart-sass for compiling SCSS.
44 ARG DART_SASS_VERSION=1.108.1
45 RUN arch=$(dpkg --print-architecture | sed 's/amd64/x64/') && \
46     curl -fsSL "https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-${arch}.tar.gz" \
47     | tar xz -C /opt
48
49 COPY requirements/requirements.txt requirements.txt
50
51 RUN --mount=type=cache,target=/root/.cache/pip \
52     pip install -r requirements.txt
53
54 RUN addgroup --gid $GID app && \
55     adduser --gid $GID --home /app --uid $UID app
56
57
58 # fonts
59 RUN cp -a /usr/local/lib/python*/site-packages/librarian/fonts /usr/share/fonts
60 RUN fc-cache
61
62 USER app
63
64 WORKDIR /app/src
65
66 RUN mkdir /app/.ipython
67
68
69 #=======#
70 #  app  #=============================================================
71 #
72 #  Code and production requirements. Not production yet: needs
73 #  staticfiles.json manifest from static-builder.
74 #
75 FROM deps AS app
76
77 USER root
78
79 RUN --mount=type=cache,target=/root/.cache/pip \
80     pip install gunicorn
81
82 USER app
83
84 COPY src /app/src
85
86
87 #==================#
88 #  static-builder  #==================================================
89 #
90 #  Static files are collected here. Statics are then copied into
91 #  the nginx image, and staticfiles.json manifest is copied back
92 #  into the test and prod images.
93 #
94 FROM app AS static-builder
95
96 # django-pipeline compiler creates temp dirs in place. Let it.
97 USER root
98 RUN find /app/src/*/static -type d -exec chown app {} +
99 USER app
100
101 RUN python manage.py collectstatic --noinput
102
103
104 ###########
105 #  nginx  ############################################################
106 #
107 #  Static files for production nginx. Config is mounted.
108 #
109 FROM nginx:alpine AS nginx
110
111 ARG UID
112 ARG GID
113
114 # COPY nginx.conf conf.d/...
115
116 RUN addgroup -g $GID app && addgroup nginx app
117 COPY --from=static-builder /app/var/static /app/var/static
118
119
120 #===============#
121 #  polish-dict  #=====================================================
122 #
123 #  Polish hunspell dictionary without diacritics, to allow
124 #  full text search without diacritics.
125 #
126 FROM ${PYTHON_IMAGE} AS polish-dict
127
128 RUN apt-get update \
129  && apt-get install -y --no-install-recommends hunspell-pl \
130  && rm -rf /var/lib/apt/lists/*
131
132 COPY scripts/fold_hunspell.py /usr/local/bin/fold_hunspell.py
133
134 RUN set -eu; \
135     mkdir /out; cd /tmp; \
136     enc="$(sed -n 's/^SET[[:space:]]*//p' /usr/share/hunspell/pl_PL.aff | tr -d '\r')"; \
137     iconv -f "${enc:-UTF-8}" -t UTF-8 /usr/share/hunspell/pl_PL.dic > pl.dict; \
138     iconv -f "${enc:-UTF-8}" -t UTF-8 /usr/share/hunspell/pl_PL.aff > pl.affix; \
139     python3 /usr/local/bin/fold_hunspell.py pl.dict pl.affix \
140         /out/polish.dict /out/polish.affix; \
141     dpkg-query -W -f='hunspell-pl ${Version}\n' hunspell-pl > /out/polish.version
142
143
144 ##############
145 #  postgres  #########################################################
146 #
147 #  Postgres with the dictionary from polish-dict.
148 #
149 FROM postgres:18 AS postgres
150
151 COPY --from=polish-dict /out/ /tmp/polish-dict/
152 RUN mv /tmp/polish-dict/* "/usr/share/postgresql/$PG_MAJOR/tsearch_data/" \
153  && rmdir /tmp/polish-dict
154
155 COPY scripts/postgres-polish.sql /docker-entrypoint-initdb.d/10-postgres-polish.sql
156
157
158 ##########
159 #  test  #############################################################
160 #
161 #  Production image plus test requirements. Defined before `prod`,
162 #  so that `prod` stays the default (last) stage.
163 #
164 FROM app AS test
165
166 USER root
167 COPY requirements/requirements-test.txt /tmp/
168 RUN --mount=type=cache,target=/root/.cache/pip \
169     pip install -r /tmp/requirements-test.txt
170 USER app
171 RUN mkdir -p /app/var/counters
172 COPY --from=static-builder /app/var/static/staticfiles.json /app/var/static/staticfiles.json
173 CMD ["./manage.py", "test"]
174
175
176 ##########
177 #  prod  #############################################################
178 #
179 #  Production image: app + staticfiles.json manifest.
180 #
181 FROM app AS prod
182
183 COPY --from=static-builder /app/var/static/staticfiles.json /app/var/static/staticfiles.json