Further reduce the size of the docker image (#3972)

Rewrite the dockerfile as a multistage build: this means we can get rid of a whole load of cruft which we don't need.
This commit is contained in:
Richard van der Hoff 2018-10-01 12:29:17 +01:00 committed by GitHub
parent 9a8bbc9a59
commit 53c5fa4e6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 25 deletions

1
changelog.d/3972.misc Normal file
View file

@ -0,0 +1 @@
Further reduce the docker image size

View file

@ -1,9 +1,13 @@
ARG PYTHON_VERSION=2 ARG PYTHON_VERSION=2
FROM docker.io/python:${PYTHON_VERSION}-alpine3.8
COPY . /synapse ###
### Stage 0: builder
###
FROM docker.io/python:${PYTHON_VERSION}-alpine3.8 as builder
RUN apk add --no-cache --virtual .build_deps \ # install the OS build deps
RUN apk add \
build-base \ build-base \
libffi-dev \ libffi-dev \
libjpeg-turbo-dev \ libjpeg-turbo-dev \
@ -11,29 +15,46 @@ RUN apk add --no-cache --virtual .build_deps \
libxslt-dev \ libxslt-dev \
linux-headers \ linux-headers \
postgresql-dev \ postgresql-dev \
zlib-dev \ zlib-dev
&& cd /synapse \
&& apk add --no-cache --virtual .runtime_deps \ # build things which have slow build steps, before we copy synapse, so that
# the layer can be cached.
#
# (we really just care about caching a wheel here, as the "pip install" below
# will install them again.)
RUN pip install --prefix="/install" --no-warn-script-location \
cryptography \
msgpack-python \
pillow \
pynacl
# now install synapse and all of the python deps to /install.
COPY . /synapse
RUN pip install --prefix="/install" --no-warn-script-location \
lxml \
psycopg2 \
/synapse
###
### Stage 1: runtime
###
FROM docker.io/python:${PYTHON_VERSION}-alpine3.8
RUN apk add --no-cache --virtual .runtime_deps \
libffi \ libffi \
libjpeg-turbo \ libjpeg-turbo \
libressl \ libressl \
libxslt \ libxslt \
libpq \ libpq \
zlib \ zlib \
su-exec \ su-exec
&& pip install --upgrade \
lxml \ COPY --from=builder /install /usr/local
pip \ COPY ./docker/start.py /start.py
psycopg2 \ COPY ./docker/conf /conf
setuptools \
&& mkdir -p /synapse/cache \
&& pip install -f /synapse/cache --upgrade --process-dependency-links . \
&& mv /synapse/docker/start.py /synapse/docker/conf / \
&& rm -rf \
setup.cfg \
setup.py \
synapse \
&& apk del .build_deps
VOLUME ["/data"] VOLUME ["/data"]