1 # Wolne Lektury images.
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.
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.
15 ARG PYTHON_IMAGE=python:3.12-trixie
21 # deps #############################################################
23 # Base system and requirements.
24 # Used directly as the dev image, with code mounted.
26 FROM ${PYTHON_IMAGE} AS deps
31 RUN apt-get update && apt-get install -y \
34 texlive-xetex texlive-lang-polish \
40 texlive-fonts-extra-links \
41 fonts-noto-core fonts-noto-extra
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" \
49 COPY requirements/requirements.txt requirements.txt
51 RUN --mount=type=cache,target=/root/.cache/pip \
52 pip install -r requirements.txt
54 RUN addgroup --gid $GID app && \
55 adduser --gid $GID --home /app --uid $UID app
59 RUN cp -a /usr/local/lib/python*/site-packages/librarian/fonts /usr/share/fonts
66 RUN mkdir /app/.ipython
70 # app #=============================================================
72 # Code and production requirements. Not production yet: needs
73 # staticfiles.json manifest from static-builder.
79 RUN --mount=type=cache,target=/root/.cache/pip \
88 # static-builder #==================================================
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.
94 FROM app AS static-builder
96 # django-pipeline compiler creates temp dirs in place. Let it.
98 RUN find /app/src/*/static -type d -exec chown app {} +
101 RUN python manage.py collectstatic --noinput
105 # nginx ############################################################
107 # Static files for production nginx. Config is mounted.
109 FROM nginx:alpine AS nginx
114 COPY scripts/nginx/nginx.conf /etc/nginx/conf.d/default.conf
115 COPY scripts/nginx/well-known /app/.well-known
117 RUN addgroup -g $GID app && addgroup nginx app
118 COPY --from=static-builder /app/var/static /app/var/static
122 # polish-dict #=====================================================
124 # Polish hunspell dictionary without diacritics, to allow
125 # full text search without diacritics.
127 FROM ${PYTHON_IMAGE} AS polish-dict
130 && apt-get install -y --no-install-recommends hunspell-pl \
131 && rm -rf /var/lib/apt/lists/*
133 COPY scripts/postgres/fold_hunspell.py /usr/local/bin/fold_hunspell.py
136 mkdir /out; cd /tmp; \
137 enc="$(sed -n 's/^SET[[:space:]]*//p' /usr/share/hunspell/pl_PL.aff | tr -d '\r')"; \
138 iconv -f "${enc:-UTF-8}" -t UTF-8 /usr/share/hunspell/pl_PL.dic > pl.dict; \
139 iconv -f "${enc:-UTF-8}" -t UTF-8 /usr/share/hunspell/pl_PL.aff > pl.affix; \
140 python3 /usr/local/bin/fold_hunspell.py pl.dict pl.affix \
141 /out/polish.dict /out/polish.affix; \
142 dpkg-query -W -f='hunspell-pl ${Version}\n' hunspell-pl > /out/polish.version
146 # postgres #########################################################
148 # Postgres with the dictionary from polish-dict.
150 FROM postgres:18 AS postgres
152 COPY --from=polish-dict /out/ /tmp/polish-dict/
153 RUN mv /tmp/polish-dict/* "/usr/share/postgresql/$PG_MAJOR/tsearch_data/" \
154 && rmdir /tmp/polish-dict
156 COPY scripts/postgres/postgres-polish.sql /docker-entrypoint-initdb.d/10-postgres-polish.sql
160 # test #############################################################
162 # Production image plus test requirements. Defined before `prod`,
163 # so that `prod` stays the default (last) stage.
168 COPY requirements/requirements-test.txt /tmp/
169 RUN --mount=type=cache,target=/root/.cache/pip \
170 pip install -r /tmp/requirements-test.txt
172 RUN mkdir -p /app/var/counters
173 COPY --from=static-builder /app/var/static/staticfiles.json /app/var/static/staticfiles.json
174 CMD ["./manage.py", "test"]
178 # prod #############################################################
180 # Production image: app + staticfiles.json manifest.
184 COPY --from=static-builder /app/var/static/staticfiles.json /app/var/static/staticfiles.json