upgrade pipeline
[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.10-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 RUN --mount=type=cache,target=/root/.cache/pip \
54     pip install \
55     psycopg2-binary \
56     django-debug-toolbar==3.2.2
57
58 RUN addgroup --gid $GID app && \
59     adduser --gid $GID --home /app --uid $UID app
60
61
62 # fonts
63 RUN cp -a /usr/local/lib/python*/site-packages/librarian/fonts /usr/share/fonts
64 RUN fc-cache
65
66 USER app
67
68 WORKDIR /app/src
69
70 RUN mkdir /app/.ipython
71
72
73 #=======#
74 #  app  #=============================================================
75 #
76 #  Code and production requirements. Not production yet: needs
77 #  staticfiles.json manifest from static-builder.
78 #
79 FROM deps AS app
80
81 USER root
82
83 RUN --mount=type=cache,target=/root/.cache/pip \
84     pip install gunicorn
85
86 USER app
87
88 COPY src /app/src
89
90
91 #==================#
92 #  static-builder  #==================================================
93 #
94 #  Static files are collected here. Statics are then copied into
95 #  the nginx image, and staticfiles.json manifest is copied back
96 #  into the test and prod images.
97 #
98 FROM app AS static-builder
99
100 RUN python manage.py collectstatic --noinput
101
102
103 ###########
104 #  nginx  ############################################################
105 #
106 #  Static files for production nginx. Config is mounted.
107 #
108 FROM nginx:alpine AS nginx
109
110 ARG UID
111 ARG GID
112
113 # COPY nginx.conf conf.d/...
114
115 RUN addgroup -g $GID app && addgroup nginx app
116 COPY --from=static-builder /app/var/static /app/var/static
117
118
119 #===============#
120 #  polish-dict  #=====================================================
121 #
122 #  Polish hunspell dictionary without diacritics, to allow
123 #  full text search without diacritics.
124 #
125 FROM ${PYTHON_IMAGE} AS polish-dict
126
127 RUN apt-get update \
128  && apt-get install -y --no-install-recommends hunspell-pl \
129  && rm -rf /var/lib/apt/lists/*
130
131 COPY scripts/fold_hunspell.py /usr/local/bin/fold_hunspell.py
132
133 RUN set -eu; \
134     mkdir /out; cd /tmp; \
135     enc="$(sed -n 's/^SET[[:space:]]*//p' /usr/share/hunspell/pl_PL.aff | tr -d '\r')"; \
136     iconv -f "${enc:-UTF-8}" -t UTF-8 /usr/share/hunspell/pl_PL.dic > pl.dict; \
137     iconv -f "${enc:-UTF-8}" -t UTF-8 /usr/share/hunspell/pl_PL.aff > pl.affix; \
138     python3 /usr/local/bin/fold_hunspell.py pl.dict pl.affix \
139         /out/polish.dict /out/polish.affix; \
140     dpkg-query -W -f='hunspell-pl ${Version}\n' hunspell-pl > /out/polish.version
141
142
143 ##############
144 #  postgres  #########################################################
145 #
146 #  Postgres with the dictionary from polish-dict.
147 #
148 FROM postgres:18 AS postgres
149
150 COPY --from=polish-dict /out/ /tmp/polish-dict/
151 RUN mv /tmp/polish-dict/* "/usr/share/postgresql/$PG_MAJOR/tsearch_data/" \
152  && rmdir /tmp/polish-dict
153
154 COPY scripts/postgres-polish.sql /docker-entrypoint-initdb.d/10-postgres-polish.sql
155
156
157 ##########
158 #  test  #############################################################
159 #
160 #  Production image plus test requirements. Defined before `prod`,
161 #  so that `prod` stays the default (last) stage.
162 #
163 FROM app AS test
164
165 USER root
166 COPY requirements/requirements-test.txt /tmp/
167 RUN --mount=type=cache,target=/root/.cache/pip \
168     pip install -r /tmp/requirements-test.txt
169 USER app
170 RUN mkdir -p /app/var/counters
171 COPY --from=static-builder /app/var/static/staticfiles.json /app/var/static/staticfiles.json
172 CMD ["./manage.py", "test"]
173
174
175 ##########
176 #  prod  #############################################################
177 #
178 #  Production image: app + staticfiles.json manifest.
179 #
180 FROM app AS prod
181
182 COPY --from=static-builder /app/var/static/staticfiles.json /app/var/static/staticfiles.json