From 144383a6e37a0f9011a560258da16cb01a90ba7d Mon Sep 17 00:00:00 2001 From: Eugene Morozov Date: Wed, 26 Apr 2023 22:05:15 +0300 Subject: [PATCH 1/2] Fixes ncurses problem and reduces image size. --- Dockerfile | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 74d273d..ab42e3d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,9 @@ -FROM python:3.9.7-alpine3.13 +FROM python:3.9.7-alpine3.13 AS builder RUN apk add --no-cache \ - curl \ # For building dependencies. \ gcc \ musl-dev \ - git \ g++ \ libffi-dev \ # For psycopg \ @@ -13,27 +11,33 @@ RUN apk add --no-cache \ # For mysql deps \ mariadb-connector-c-dev +COPY pyproject.toml poetry.lock README.md /src/ +COPY ./fastapi_template /src/fastapi_template/ +WORKDIR /src + +RUN python -m venv --copies /src/venv +RUN /src/venv/bin/pip install poetry==1.4.2 +RUN --mount=type=cache,target=/root/.cache/pypoetry \ + . /src/venv/bin/activate && poetry install + +FROM python:3.9.7-alpine3.13 + +RUN apk add --no-cache git ncurses RUN adduser --disabled-password fastapi_template RUN mkdir /projects /src RUN chown -R fastapi_template:fastapi_template /projects /src USER fastapi_template +COPY --from=builder /src/venv /src/venv/ +COPY ./fastapi_template /src/fastapi_template/ WORKDIR /src -ENV PATH ${PATH}:/home/fastapi_template/.local/bin - -RUN pip install poetry==1.4.2 - -COPY . /src/ -RUN pip install . +ENV PATH ${PATH}:/src/venv/bin -USER root -RUN rm -rfv /src -RUN apk del curl USER fastapi_template VOLUME /projects WORKDIR /projects -ENTRYPOINT ["/home/fastapi_template/.local/bin/fastapi_template"] +ENTRYPOINT ["/src/venv/bin/fastapi_template"] From b0dd4aa72ed05cbed5ca78188c6459b032179437 Mon Sep 17 00:00:00 2001 From: Eugene Morozov Date: Wed, 26 Apr 2023 22:14:41 +0300 Subject: [PATCH 2/2] Shaves off additional 7 Mb from the final image size. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ab42e3d..8579193 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ WORKDIR /src RUN python -m venv --copies /src/venv RUN /src/venv/bin/pip install poetry==1.4.2 RUN --mount=type=cache,target=/root/.cache/pypoetry \ - . /src/venv/bin/activate && poetry install + . /src/venv/bin/activate && poetry install --only main FROM python:3.9.7-alpine3.13