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 @@
########################################## # frontent builder stage ---------------------------------------------
## build frontend FROM node:20.3-alpine3.18 as frontend_builder
##########################################
FROM node:buster as front_build RUN mkdir /app
# FROM node as front_build WORKDIR /app
RUN mkdir /front COPY . /app
WORKDIR /front
COPY . /front/ RUN yarn
RUN yarn RUN npx update-browserslist-db@latest -y
RUN npx update-browserslist-db@latest -y RUN yarn css
RUN yarn css RUN yarn js
RUN yarn js
# backend builder stage ----------------------------------------------
FROM python:3.11-alpine as backend_builder
FROM python:3.11-alpine
ENV VIRTUAL_ENV=/app/venv
# install build utils and dependencies ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN apk update && apk upgrade ENV PYTHONFAULTHANDLER=1
RUN apk add --no-cache pkgconfig \ ENV PYTHONUNBUFFERED=1
gcc \ ENV PYTHONHASHSEED=random
musl-dev \ ENV PIP_NO_CACHE_DIR=off
libffi-dev \ ENV PIP_DISABLE_PIP_VERSION_CHECK=on
&& rm -rf /var/cache/apk/*
RUN mkdir /app
WORKDIR /app
# Add user app COPY . /app
RUN python -m pip install -U pip
RUN adduser -D app RUN apk update \
USER app && apk add --no-cache pkgconfig gcc musl-dev libffi-dev \
WORKDIR /home/app && rm -rf /var/cache/apk/*
# set environment varibles RUN python -m venv $VIRTUAL_ENV \
ENV PYTHONFAULTHANDLER 1 && pip install poetry
ENV PYTHONUNBUFFERED 1
ENV PYTHONHASHSEED random RUN poetry install --only main --no-interaction --no-ansi \
ENV PIP_NO_CACHE_DIR off && poetry add gunicorn
ENV PIP_DISABLE_PIP_VERSION_CHECK on
# final image stage --------------------------------------------------
# install poetry FROM python:3.11-alpine as final
RUN pip install --user poetry
ENV PATH="/home/app/.local/bin:${PATH}" ENV VIRTUAL_ENV=/app/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
# install app dependencies
COPY --chown=app:app poetry.lock . RUN mkdir /app
COPY --chown=app:app pyproject.toml . WORKDIR /app
COPY --chown=app:app poetry.toml .
RUN adduser -D app
RUN poetry install --no-dev --no-interaction --no-ansi USER app
# add gunicorn
RUN poetry add gunicorn RUN pip install poetry
COPY --chown=app:app . . COPY --chown=app:app --from=frontend_builder /app/app/static ./static
COPY --chown=app:app --from=front_build /front/app/static ./app/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 # echo Run app
# flask run -h 0.0.0.0 # flask run -h 0.0.0.0
echo Run app server 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' $@