From 9391e6b697e9f50ccbad9f65d6e7d30446046623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gre=CC=81goire=20Compagnon?= Date: Thu, 7 Mar 2024 11:45:50 +0100 Subject: [PATCH] chore(Dockerfile): improve caching strategy for dependency installation --- Dockerfile | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index 335fa88..74228ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,14 +3,18 @@ FROM python:3.12 as builder -# Remove docker clean process -RUN rm /etc/apt/apt.conf.d/docker-clean - -# Set non interactive mode +# Set non-interactive mode ENV DEBIAN_FRONTEND=noninteractive -# Update and install dependencies -RUN --mount=type=cache,target=/var/cache/apt --mount=type=cache,target=/var/lib/apt \ +# Prevent docker from cleaning up the apt cache +RUN rm -f /etc/apt/apt.conf.d/docker-clean + +# Define ARG for platform-specific cache separation +ARG TARGETPLATFORM + +# Update and install dependencies with cache separated by architecture +RUN --mount=type=cache,target=/var/cache/apt,id=apt-cache-${TARGETPLATFORM} \ + --mount=type=cache,target=/var/lib/apt,id=apt-lib-${TARGETPLATFORM} \ apt-get update && \ apt-get install -y libxml2-dev libxslt-dev @@ -18,46 +22,43 @@ WORKDIR /app COPY requirements.txt . -RUN --mount=type=cache,target=/root/.cache \ +# Use pip cache to speed up builds +RUN --mount=type=cache,target=/root/.cache/pip \ pip install -r requirements.txt -t packages -# Start from a lightweight Python 3.12 image to ensure a small final image size +# Start from a slim Python 3.12 image for a small final image size FROM python:3.12-slim as final +# Set non-interactive mode +ENV DEBIAN_FRONTEND=noninteractive -# Copy built packages from the previous stage (Do it first to force that the building of this stage wait for the previous stage to finish and avoid a race condition) -COPY --from=builder /app/packages /app/packages +# Prevent docker from cleaning up the apt cache in the final image +RUN rm -f /etc/apt/apt.conf.d/docker-clean -# Remove docker clean process -RUN rm /etc/apt/apt.conf.d/docker-clean +ARG TARGETPLATFORM -# Set non interactive mode -ENV DEBIAN_FRONTEND=noninteractive +# Copy built packages from the previous stage +COPY --from=builder /app/packages /app/packages -# Update and install dependencies -RUN --mount=type=cache,target=/var/cache/apt --mount=type=cache,target=/var/lib/apt \ +# Update and install runtime dependencies if necessary, with cache separated by architecture +RUN --mount=type=cache,target=/var/cache/apt,id=apt-cache-${TARGETPLATFORM} \ + --mount=type=cache,target=/var/lib/apt,id=apt-lib-${TARGETPLATFORM} \ apt-get update && \ apt-get install -y libxml2 libxslt1.1 -# Set the working directory inside the container to /app WORKDIR /app ENV PYTHONPATH=/app/packages:$PYTHONPATH -# Copy the requirements.txt file into the working directory COPY requirements.txt . -# Install dependencies from requirements.txt using pip -# Utilize Docker's cache mount feature to speed up builds by caching pip's cache directory -RUN --mount=type=cache,target=/root/.cache \ +# Install dependencies from requirements.txt using pip and cache +RUN --mount=type=cache,target=/root/.cache/pip \ pip install -r requirements.txt # Copy the rest of the application's source code into the working directory COPY . . -# Declare a volume at /app/cache to allow for caching or persisting data outside of the container VOLUME [ "/app/cache"] -# Set the container's entrypoint to run the main Python application ENTRYPOINT [ "python", "main.py" ] -