From af08628c7470392334b19bbf16933091cbee4cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Fri, 30 Jun 2023 15:42:29 +0200 Subject: [PATCH] Dockerfile: use Python venv, improve image size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reduced size by ~180 MB: ``` > d images | grep statusteam/logos-open-law statusteam/logos-open-law new e547a1a95495 24 hours ago 312MB statusteam/logos-open-law old c3d18cb98d82 45 hours ago 494MB ``` Signed-off-by: Jakub SokoĊ‚owski --- Dockerfile | 108 ++++++++++++++++++++++++------------------------ start_server.sh | 2 +- 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/Dockerfile b/Dockerfile index b2d342e..a7bd608 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,53 +1,55 @@ -########################################## -## build frontend -########################################## -FROM node:buster as front_build -# FROM node as front_build -RUN mkdir /front -WORKDIR /front -COPY . /front/ -RUN yarn -RUN npx update-browserslist-db@latest -y -RUN yarn css -RUN yarn js - - -FROM python:3.11-alpine - -# install build utils and dependencies -RUN apk update && apk upgrade -RUN apk add --no-cache pkgconfig \ - gcc \ - musl-dev \ - libffi-dev \ - && rm -rf /var/cache/apk/* - - -# Add user app -RUN python -m pip install -U pip -RUN adduser -D app -USER app -WORKDIR /home/app - -# set environment varibles -ENV PYTHONFAULTHANDLER 1 -ENV PYTHONUNBUFFERED 1 -ENV PYTHONHASHSEED random -ENV PIP_NO_CACHE_DIR off -ENV PIP_DISABLE_PIP_VERSION_CHECK on - -# install poetry -RUN pip install --user poetry -ENV PATH="/home/app/.local/bin:${PATH}" - -# install app dependencies -COPY --chown=app:app poetry.lock . -COPY --chown=app:app pyproject.toml . -COPY --chown=app:app poetry.toml . - -RUN poetry install --no-dev --no-interaction --no-ansi -# add gunicorn -RUN poetry add gunicorn - -COPY --chown=app:app . . -COPY --chown=app:app --from=front_build /front/app/static ./app/static +# frontent builder stage --------------------------------------------- +FROM node:20.3-alpine3.18 as frontend_builder + +RUN mkdir /app +WORKDIR /app +COPY . /app + +RUN yarn +RUN npx update-browserslist-db@latest -y +RUN yarn css +RUN yarn js + +# backend builder stage ---------------------------------------------- +FROM python:3.11-alpine as backend_builder + +ENV VIRTUAL_ENV=/app/venv +ENV PATH="$VIRTUAL_ENV/bin:$PATH" +ENV PYTHONFAULTHANDLER=1 +ENV PYTHONUNBUFFERED=1 +ENV PYTHONHASHSEED=random +ENV PIP_NO_CACHE_DIR=off +ENV PIP_DISABLE_PIP_VERSION_CHECK=on + +RUN mkdir /app +WORKDIR /app +COPY . /app + +RUN apk update \ + && apk add --no-cache pkgconfig gcc musl-dev libffi-dev \ + && rm -rf /var/cache/apk/* + +RUN python -m venv $VIRTUAL_ENV \ + && pip install poetry + +RUN poetry install --only main --no-interaction --no-ansi \ + && poetry add gunicorn + +# final image stage -------------------------------------------------- +FROM python:3.11-alpine as final + +ENV VIRTUAL_ENV=/app/venv +ENV PATH="$VIRTUAL_ENV/bin:$PATH" + +RUN mkdir /app +WORKDIR /app + +RUN adduser -D app +USER app + +RUN pip install poetry + +COPY --chown=app:app --from=frontend_builder /app/app/static ./static +COPY --chown=app:app --from=backend_builder /app ./ + +ENTRYPOINT ["/bin/sh", "./start_server.sh"] diff --git a/start_server.sh b/start_server.sh index 958a8df..81198cb 100644 --- a/start_server.sh +++ b/start_server.sh @@ -4,4 +4,4 @@ poetry run flask db upgrade # echo Run app # flask run -h 0.0.0.0 echo Run app server -poetry run gunicorn -w 4 -b 0.0.0.0 'wsgi:app' +poetry run gunicorn -w 4 -b 0.0.0.0 'wsgi:app' $@