Skip to content

Commit

Permalink
chore(Dockerfile): improve caching strategy for dependency installation
Browse files Browse the repository at this point in the history
  • Loading branch information
obeone committed Mar 7, 2024
1 parent 5e9fb57 commit 9391e6b
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,62 @@

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

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" ]

0 comments on commit 9391e6b

Please sign in to comment.