Dockerfile: use Python venv, improve image size

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 <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2023-06-30 15:42:29 +02:00
parent eb7a6df35d
commit af08628c74
No known key found for this signature in database
GPG Key ID: FE65CD384D5BF7B4
2 changed files with 56 additions and 54 deletions

View File

@ -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"]

View File

@ -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' $@