build: optimize Dockerfile for better layer caching and smaller image size

- Combined apt-get commands into a single RUN layer
- Added --no-install-recommends flag to avoid unnecessary packages
- Cleaned up apt cache and removed package lists after installation
- Reordered COPY commands to copy requirements.txt first for better Docker layer caching
- Separated COPY of requirements.txt from the rest of the application code
This commit is contained in:
Juan José Gutiérrez de Quevedo Pérez 2025-11-21 18:49:15 +01:00
parent df78bab6f4
commit 7614131511

View file

@ -3,13 +3,25 @@ FROM python:3-bookworm
# Set working directory # Set working directory
WORKDIR /app WORKDIR /app
# Install runtime dependencies (minimal) # Install runtime dependencies in a single layer and clean up apt cache
RUN apt update && apt install cvs rsh-client cvsps RUN apt-get update && \
apt-get install -y --no-install-recommends \
cvs \
rsh-client \
cvsps && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
COPY . . # Copy requirements first for better layer caching
COPY requirements.txt .
# Install Python dependencies # Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application
COPY . .
# Install the package
RUN pip install --no-cache-dir -e . RUN pip install --no-cache-dir -e .
# Set environment variables for configuration (with defaults) # Set environment variables for configuration (with defaults)