From 44110b1f6a8f4566d9ca1c71695a799e5a22f588 Mon Sep 17 00:00:00 2001 From: Oleksander Piskun Date: Wed, 26 Mar 2025 12:39:48 +0200 Subject: [PATCH 1/3] cache python packages for speedup(DOCKER_BUILDKIT=1) Signed-off-by: Oleksander Piskun --- Dockerfile | 6 +++--- Makefile | 2 +- requirements.txt | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8c2b637..3fa183c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.11-slim-bookworm +FROM python:3.12-slim-bookworm RUN apt-get update && apt-get install -y curl procps && \ apt-get clean && \ @@ -6,8 +6,8 @@ RUN apt-get update && apt-get install -y curl procps && \ COPY requirements.txt / -RUN \ - python3 -m pip install -r requirements.txt && rm -rf ~/.cache && rm requirements.txt +RUN --mount=type=cache,target=/root/.cache/pip \ + python3 -m pip install -r requirements.txt && rm requirements.txt ADD /ex_app/cs[s] /ex_app/css ADD /ex_app/im[g] /ex_app/img diff --git a/Makefile b/Makefile index a904b9a..f82273d 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ help: .PHONY: build-push build-push: docker login ghcr.io - docker buildx build --push --platform linux/arm64/v8,linux/amd64 --tag ghcr.io/nextcloud/app-skeleton-python:latest . + DOCKER_BUILDKIT=1 docker buildx build --push --platform linux/arm64/v8,linux/amd64 --tag ghcr.io/nextcloud/app-skeleton-python:latest . .PHONY: run30 run29: diff --git a/requirements.txt b/requirements.txt index caac5a9..ccef928 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -nc_py_api[app]>=0.19.1 +nc_py_api[app]>=0.19.2 From 23d2ce114775a4eaa82ebbc88153beda980d39c1 Mon Sep 17 00:00:00 2001 From: Oleksander Piskun Date: Wed, 26 Mar 2025 14:03:37 +0200 Subject: [PATCH 2/3] user Docker Stage Build Signed-off-by: Oleksander Piskun --- Dockerfile | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3fa183c..1c64e0e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,29 @@ -FROM python:3.12-slim-bookworm +############################# +# Stage 1: Builder +############################# +FROM python:3.12-slim-bookworm AS builder -RUN apt-get update && apt-get install -y curl procps && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* +# Install build dependencies +RUN apt-get update && apt-get install -y curl && \ + apt-get clean && rm -rf /var/lib/apt/lists/* +# Copy requirements and install Python dependencies using a cache mount. COPY requirements.txt / - RUN --mount=type=cache,target=/root/.cache/pip \ - python3 -m pip install -r requirements.txt && rm requirements.txt + python3 -m pip install -r requirements.txt && rm requirements.txt +# Add application files (using your ADD patterns) ADD /ex_app/cs[s] /ex_app/css ADD /ex_app/im[g] /ex_app/img ADD /ex_app/j[s] /ex_app/js ADD /ex_app/l10[n] /ex_app/l10n ADD /ex_app/li[b] /ex_app/lib +# Copy scripts with the proper permissions. COPY --chmod=775 healthcheck.sh / COPY --chmod=775 start.sh / -# Download and install FRP client +# Download and install FRP client into /usr/local/bin. RUN set -ex; \ ARCH=$(uname -m); \ if [ "$ARCH" = "aarch64" ]; then \ @@ -34,6 +39,23 @@ RUN set -ex; \ chmod +x /usr/local/bin/frpc; \ rm -rf /tmp/frp /tmp/frp.tar.gz +############################# +# Stage 2: Final Runtime Image +############################# +FROM python:3.12-slim-bookworm + +# Install any runtime apt packages your app needs. +RUN apt-get update && apt-get install -y curl procps && \ + apt-get clean && rm -rf /var/lib/apt/lists/* + +# Copy installed Python packages and FRP client from the builder. +COPY --from=builder /usr/local/ /usr/local/ +# Copy application files and scripts from the builder. +COPY --from=builder /ex_app/ /ex_app/ +COPY --from=builder /healthcheck.sh /healthcheck.sh +COPY --from=builder /start.sh /start.sh + +# Set working directory and define entrypoint/healthcheck. WORKDIR /ex_app/lib ENTRYPOINT ["/start.sh"] HEALTHCHECK --interval=2s --timeout=2s --retries=300 CMD /healthcheck.sh From ca73874c29c3f06848ad96d6b1375ac90af06081 Mon Sep 17 00:00:00 2001 From: Oleksander Piskun Date: Wed, 26 Mar 2025 15:24:42 +0200 Subject: [PATCH 3/3] applied corrections from the review Signed-off-by: Oleksander Piskun --- Dockerfile | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1c64e0e..0fc5ebe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,18 +10,7 @@ RUN apt-get update && apt-get install -y curl && \ # Copy requirements and install Python dependencies using a cache mount. COPY requirements.txt / RUN --mount=type=cache,target=/root/.cache/pip \ - python3 -m pip install -r requirements.txt && rm requirements.txt - -# Add application files (using your ADD patterns) -ADD /ex_app/cs[s] /ex_app/css -ADD /ex_app/im[g] /ex_app/img -ADD /ex_app/j[s] /ex_app/js -ADD /ex_app/l10[n] /ex_app/l10n -ADD /ex_app/li[b] /ex_app/lib - -# Copy scripts with the proper permissions. -COPY --chmod=775 healthcheck.sh / -COPY --chmod=775 start.sh / + python3 -m pip install --root-user-action=ignore -r requirements.txt && rm requirements.txt # Download and install FRP client into /usr/local/bin. RUN set -ex; \ @@ -44,16 +33,23 @@ RUN set -ex; \ ############################# FROM python:3.12-slim-bookworm +# Copy installed Python packages and FRP client from the builder. +COPY --from=builder /usr/local/ /usr/local/ + # Install any runtime apt packages your app needs. RUN apt-get update && apt-get install -y curl procps && \ apt-get clean && rm -rf /var/lib/apt/lists/* -# Copy installed Python packages and FRP client from the builder. -COPY --from=builder /usr/local/ /usr/local/ -# Copy application files and scripts from the builder. -COPY --from=builder /ex_app/ /ex_app/ -COPY --from=builder /healthcheck.sh /healthcheck.sh -COPY --from=builder /start.sh /start.sh +# Add application files. +ADD /ex_app/cs[s] /ex_app/css +ADD /ex_app/im[g] /ex_app/img +ADD /ex_app/j[s] /ex_app/js +ADD /ex_app/l10[n] /ex_app/l10n +ADD /ex_app/li[b] /ex_app/lib + +# Copy scripts with the proper permissions. +COPY --chmod=775 healthcheck.sh / +COPY --chmod=775 start.sh / # Set working directory and define entrypoint/healthcheck. WORKDIR /ex_app/lib