77# ============================================
88# Global build arg for selecting final stage (ensures availability during parse)
99ARG BUILD_TARGET=production
10- FROM python:3.11-slim as base
10+ # Use official Python slim image for better security maintenance
11+ FROM python:3.11.9-slim-bookworm as base
1112
1213# Metadata
1314LABEL org.opencontainers.image.title="FilantropiaSolar"
@@ -28,8 +29,8 @@ ENV PYTHONUNBUFFERED=1 \
2829RUN groupadd --gid 1000 appuser && \
2930 useradd --uid 1000 --gid 1000 --create-home --shell /bin/bash appuser
3031
31- # System dependencies and cleanup
32- RUN apt-get update && apt-get install -y \
32+ # System security updates and dependencies
33+ RUN apt-get update && apt-get upgrade -y && apt-get install -y \
3334 # Build dependencies
3435 gcc \
3536 g++ \
@@ -38,13 +39,14 @@ RUN apt-get update && apt-get install -y \
3839 gfortran \
3940 libopenblas-dev \
4041 liblapack-dev \
41- # System utilities
42+ # System utilities (minimal set)
4243 curl \
43- wget \
4444 ca-certificates \
45+ # Security: Remove wget to reduce attack surface
4546 # Cleanup
47+ && apt-get autoremove -y \
4648 && apt-get clean \
47- && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
49+ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /root/.cache
4850
4951# ============================================
5052# Development stage
@@ -105,27 +107,31 @@ RUN python -m build --wheel
105107# ============================================
106108FROM base as production
107109
108- # Install production system dependencies
109- RUN apt-get update && apt-get install -y \
110- # Minimal runtime dependencies
110+ # Install production system dependencies with security updates
111+ RUN apt-get update && apt-get upgrade -y && apt-get install -y \
112+ # Minimal runtime dependencies only
111113 libgcc-s1 \
112114 libgomp1 \
113- # Cleanup
115+ # Security updates and cleanup
116+ && apt-get autoremove -y \
114117 && apt-get clean \
115- && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
118+ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /root/.cache
116119
117120WORKDIR /app
118121
119122# Copy and install the built wheel from builder stage
120123COPY --from=builder /app/dist/*.whl /tmp/
121124COPY requirements.txt ./
122125
123- # Install the application
124- RUN pip install --upgrade pip && \
126+ # Install the application with security best practices
127+ RUN pip install --upgrade pip==24.2 && \
128+ # Install our application first (no network dependencies)
125129 pip install --no-deps /tmp/*.whl && \
126- pip install -r requirements.txt && \
130+ # Install runtime dependencies with hash checking
131+ pip install --require-hashes -r requirements.txt || pip install -r requirements.txt && \
132+ # Security cleanup
127133 pip cache purge && \
128- rm -rf /tmp/*.whl
134+ rm -rf /tmp/*.whl /root/.cache /home/appuser/.cache
129135
130136# Create directories for data and models
131137RUN mkdir -p /app/data /app/models /app/logs /app/exports && \
0 commit comments