Skip to content

Commit 0ca5021

Browse files
Slim down production Django container
Heavily baed on Anthony Ledesma's PR (GhostManager#163), this builds the Python wheels in a separate stage, so that the published docker image does not have the packages for building or the source codes taking up space. The Django image has shrunk from roughly 2gb to 400mb.
1 parent f0de1e5 commit 0ca5021

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

compose/local/django/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ RUN apk --no-cache add build-base curl \
2525

2626
COPY ./requirements /requirements
2727

28-
RUN pip install --no-cache-dir -r /requirements/local.txt
28+
RUN pip install --no-cache-dir -r /requirements/local.txt --no-binary psycopg2
2929

3030
COPY ./compose/production/django/entrypoint /entrypoint
3131

compose/production/django/Dockerfile

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,41 @@ FROM node:21-alpine AS frontendbuild
33
COPY ./javascript /app
44
RUN --mount=type=cache,target=/root/.npm cd /app && npm ci && npm run build-frontend-prod
55

6-
FROM python:3.10.9-alpine3.17
7-
8-
ENV PYTHONUNBUFFERED=1
9-
10-
ENV PYTHONPATH="$PYTHONPATH:/app/config"
6+
FROM python:3.10.9-alpine3.17 AS build
117

8+
COPY ./requirements /requirements
129
RUN \
1310
--mount=type=cache,target=/var/cache/apk/ \
14-
--mount=type=cache,target=/root/.cache/pip \
1511
apk add build-base curl \
1612
# psycopg2 dependencies
1713
&& apk add --virtual build-deps gcc python3-dev musl-dev \
18-
&& apk add postgresql-dev \
14+
&& apk add libpq-dev \
1915
# Pillow dependencies
2016
&& apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
2117
# CFFI dependencies
2218
&& apk add libffi-dev py-cffi \
2319
# XLSX dependencies
2420
&& apk add libxml2-dev libxslt-dev \
2521
# Rust and Cargo required by the ``cryptography`` Python package
26-
&& apk add rust cargo \
27-
&& addgroup -S django \
28-
&& adduser -S -G django django \
29-
&& pip install -U setuptools pip
22+
&& apk add rust cargo
23+
RUN \
24+
--mount=type=cache,target=/root/.cache/pip \
25+
pip install wheel \
26+
&& pip wheel --wheel-dir=/wheels --no-binary psycopg2 -r /requirements/production.txt
3027

31-
COPY ./requirements /requirements
28+
FROM python:3.10.9-alpine3.17
29+
30+
RUN \
31+
--mount=type=cache,target=/var/cache/apk/ \
32+
apk add libpq
33+
34+
RUN \
35+
--mount=type=bind,target=/wheels,source=/wheels,from=build \
36+
--mount=type=bind,target=/requirements,source=/requirements,from=build \
37+
--mount=type=cache,target=/root/.cache/pip \
38+
pip install --find-links=/wheels -r /requirements/production.txt
3239

33-
RUN --mount=type=cache,target=/root/.cache/pip pip install -r /requirements/production.txt \
34-
&& rm -rf /requirements
40+
RUN addgroup -S django && adduser -S -G django django
3541

3642
COPY ./compose/production/django/entrypoint /entrypoint
3743

requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ djangorestframework==3.15.2 # https://github.com/encode/django-rest-framework
2222
django-health-check==3.18.3 # https://github.com/KristianOellegaard/django-health-check
2323

2424
# Ghostwriter specific
25+
psycopg2==2.9.6 # https://github.com/psycopg/psycopg2
2526
boto3==1.35.17
2627
botocore==1.35.17
2728
beautifulsoup4==4.12.3

requirements/local.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Werkzeug[watchdog]==2.3.4 # https://github.com/pallets/werkzeug
44
ipdb==0.13.13 # https://github.com/gotcha/ipdb
55
Sphinx==3.2.1 # https://github.com/sphinx-doc/sphinx
6-
psycopg2==2.9.6 --no-binary psycopg2 # https://github.com/psycopg/psycopg2
76
watchgod==0.8.2 # https://github.com/samuelcolvin/watchgod
87

98
# Testing

requirements/production.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
-r ./base.txt
44

55
gunicorn==20.1.0 # https://github.com/benoitc/gunicorn
6-
psycopg2==2.9.3 --no-binary psycopg2 # https://github.com/psycopg/psycopg2
76
Collectfast==2.2.0 # https://github.com/antonagestam/collectfast
87

98
# Django

0 commit comments

Comments
 (0)