44# ============================================================================
55# Stage 1: Builder - Install dependencies
66# ============================================================================
7- FROM python:3.11-slim as builder
7+ FROM python:3.11-slim AS builder
88
99# Set working directory
1010WORKDIR /build
@@ -16,20 +16,29 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1616 libpq-dev \
1717 && rm -rf /var/lib/apt/lists/*
1818
19- # Copy requirements and install Python dependencies
19+ # Copy requirements and install Python dependencies to /opt/venv
20+ RUN python -m venv /opt/venv
21+ ENV PATH="/opt/venv/bin:$PATH"
22+
2023COPY requirements.txt .
2124RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
22- pip install --no-cache-dir --user - r requirements.txt
25+ pip install --no-cache-dir -r requirements.txt
2326
2427# ============================================================================
2528# Stage 2: Runtime - Minimal production image
2629# ============================================================================
2730FROM python:3.11-slim
2831
32+ # Build arguments for metadata
33+ ARG BUILD_DATE
34+ ARG VCS_REF
35+
2936# Set labels for image metadata
3037LABEL maintainer="NeuroBank Development Team <dev@neurobank.com>"
3138LABEL description="NeuroBank FastAPI Banking System - Production API"
3239LABEL version="1.0.0"
40+ LABEL org.opencontainers.image.created="${BUILD_DATE}"
41+ LABEL org.opencontainers.image.revision="${VCS_REF}"
3342
3443# Set working directory
3544WORKDIR /app
@@ -41,8 +50,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
4150 && rm -rf /var/lib/apt/lists/* \
4251 && apt-get clean
4352
44- # Copy Python dependencies from builder stage
45- COPY --from=builder /root/.local /root/.local
53+ # Copy Python virtual environment from builder stage
54+ COPY --from=builder /opt/venv /opt/venv
4655
4756# Copy application code
4857COPY ./app ./app
@@ -63,7 +72,7 @@ ENV PYTHONPATH=/app \
6372 PYTHONDONTWRITEBYTECODE=1 \
6473 PORT=8000 \
6574 ENVIRONMENT=production \
66- PATH=/root/.local /bin:$PATH
75+ PATH="/opt/venv /bin:$PATH"
6776
6877# Expose port (will be overridden by $PORT in cloud environments)
6978EXPOSE 8000
@@ -73,12 +82,4 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
7382 CMD curl -f http://localhost:${PORT:-8000}/health || exit 1
7483
7584# Run the application with uvicorn
76- # Use shell form to allow environment variable expansion
77- CMD uvicorn app.main:app \
78- --host 0.0.0.0 \
79- --port ${PORT:-8000} \
80- --workers ${WORKERS:-1} \
81- --loop uvloop \
82- --timeout-keep-alive 120 \
83- --access-log \
84- --log-level info
85+ CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000} --workers ${WORKERS:-1} --loop uvloop --timeout-keep-alive 120 --access-log --log-level info"]
0 commit comments