From 1bee84d419f3806a6ff30e30b90557b30b6a74b2 Mon Sep 17 00:00:00 2001 From: michael1011 <me@michael1011.at> Date: Wed, 10 Jul 2024 14:22:44 +0200 Subject: [PATCH] chore: optimize Dockerfile (#2576) By doing a multi-stage Docker build, the size of the final image is reduced by ~45%. --- .dockerignore | 1 + Dockerfile | 45 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/.dockerignore b/.dockerignore index 7e3cbdb12..4cdac6132 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,6 +3,7 @@ data docker docs tests +node_modules lnbits/static/css/* lnbits/static/bundle.js diff --git a/Dockerfile b/Dockerfile index c5806395b..d223e8f1c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,25 +1,50 @@ -FROM python:3.10-slim-bookworm +FROM python:3.10-slim-bookworm AS builder RUN apt-get clean RUN apt-get update -RUN apt-get install -y curl pkg-config build-essential libnss-myhostname automake libtool libffi-dev libgmp-dev +RUN apt-get install -y curl pkg-config build-essential libnss-myhostname RUN curl -sSL https://install.python-poetry.org | python3 - ENV PATH="/root/.local/bin:$PATH" +WORKDIR /app + +# Only copy the files required to install the dependencies +COPY pyproject.toml poetry.lock ./ + +RUN mkdir data + +ENV POETRY_NO_INTERACTION=1 \ + POETRY_VIRTUALENVS_IN_PROJECT=1 \ + POETRY_VIRTUALENVS_CREATE=1 \ + POETRY_CACHE_DIR=/tmp/poetry_cache + +RUN poetry install --only main + +FROM python:3.10-slim-bookworm + # needed for backups postgresql-client version 14 (pg_dump) -RUN apt install -y postgresql-common ca-certificates -RUN install -d /usr/share/postgresql-common/pgdg -RUN curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc -RUN sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt bookworm-pgdg main" > /etc/apt/sources.list.d/pgdg.list' -RUN apt-get update -RUN apt-get install -y postgresql-client-14 +RUN apt-get update && apt-get -y upgrade && \ + apt-get -y install gnupg2 curl lsb-release && \ + sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \ + curl -s https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \ + apt-get update && \ + apt-get -y install postgresql-client-14 postgresql-client-common && \ + apt-get clean all && rm -rf /var/lib/apt/lists/* + +RUN curl -sSL https://install.python-poetry.org | python3 - +ENV PATH="/root/.local/bin:$PATH" + +ENV POETRY_NO_INTERACTION=1 \ + POETRY_VIRTUALENVS_IN_PROJECT=1 \ + POETRY_VIRTUALENVS_CREATE=1 \ + VIRTUAL_ENV=/app/.venv \ + PATH="/app/.venv/bin:$PATH" WORKDIR /app COPY . . - -RUN mkdir data +COPY --from=builder /app/.venv .venv RUN poetry install --only main