Use BuildKit cache mounts and multi-stage build for the Docker image
CI / Build and push docker image (push) Successful in 2m33s
CI / Build and push docker image (push) Successful in 2m33s
Split the Dockerfile into a builder stage (apk + pip cache mounts) and a slim runtime stage, mirroring reimpasto. apk and pip package caches now persist across builds and never land in the image layers.
This commit is contained in:
+32
-17
@@ -1,31 +1,46 @@
|
|||||||
FROM alpine:3.24
|
# syntax=docker/dockerfile:1
|
||||||
|
# Multi-stage production build for the kaya-rbcs app on the Kaya framework.
|
||||||
|
# Base: alpine:3.24 (python3 = 3.14). Deps are installed into a venv in the
|
||||||
|
# builder; apk and pip caches use BuildKit cache mounts so they persist across
|
||||||
|
# builds and never land in the image layers. Requires BuildKit (default for
|
||||||
|
# `docker build` / `docker buildx` on modern daemons).
|
||||||
|
|
||||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
# --- Builder ---------------------------------------------------------------
|
||||||
PYTHONUNBUFFERED=1 \
|
FROM alpine:3.24 AS builder
|
||||||
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
|
||||||
PIP_NO_CACHE_DIR=1 \
|
|
||||||
RBCS_HOST=0.0.0.0 \
|
|
||||||
RBCS_PORT=8080 \
|
|
||||||
RBCS_MEMCACHE_HOST=memcached \
|
|
||||||
RBCS_MEMCACHE_PORT=11211
|
|
||||||
|
|
||||||
RUN apk add --no-cache python3
|
RUN --mount=type=cache,target=/var/cache/apk \
|
||||||
|
apk add python3
|
||||||
|
|
||||||
WORKDIR /opt/kaya-rbcs
|
WORKDIR /build
|
||||||
|
|
||||||
COPY requirements.txt ./
|
COPY requirements.txt ./
|
||||||
RUN python3 -m venv /opt/venv \
|
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||||
|
python3 -m venv /opt/venv \
|
||||||
&& /opt/venv/bin/pip install -r requirements.txt
|
&& /opt/venv/bin/pip install -r requirements.txt
|
||||||
|
|
||||||
COPY pyproject.toml README.md ./
|
COPY pyproject.toml README.md ./
|
||||||
COPY src ./src
|
COPY src ./src
|
||||||
RUN /opt/venv/bin/pip install --no-deps .
|
|
||||||
|
|
||||||
RUN addgroup -S rbcs \
|
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||||
&& adduser -S -G rbcs rbcs \
|
/opt/venv/bin/pip install --no-deps .
|
||||||
&& chown -R rbcs:rbcs /opt/kaya-rbcs /opt/venv
|
|
||||||
|
|
||||||
ENV PATH="/opt/venv/bin:${PATH}"
|
# --- Runtime ---------------------------------------------------------------
|
||||||
|
FROM alpine:3.24
|
||||||
|
|
||||||
|
RUN --mount=type=cache,target=/var/cache/apk \
|
||||||
|
apk add python3 ca-certificates tzdata \
|
||||||
|
&& addgroup -S rbcs && adduser -S -G rbcs rbcs
|
||||||
|
|
||||||
|
COPY --from=builder /opt/venv /opt/venv
|
||||||
|
|
||||||
|
ENV PATH="/opt/venv/bin:${PATH}" \
|
||||||
|
PYTHONUNBUFFERED=1 \
|
||||||
|
PYTHONDONTWRITEBYTECODE=1 \
|
||||||
|
RBCS_HOST=0.0.0.0 \
|
||||||
|
RBCS_PORT=8080 \
|
||||||
|
RBCS_MEMCACHE_HOST=memcached \
|
||||||
|
RBCS_MEMCACHE_PORT=11211
|
||||||
|
|
||||||
USER rbcs
|
USER rbcs
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user