-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDockerfile.prod
More file actions
99 lines (79 loc) · 2.75 KB
/
Copy pathDockerfile.prod
File metadata and controls
99 lines (79 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Production Dockerfile for CAE (Conversational Analysis Engine)
# This is optimized for production with security hardening and minimal size
# Stage 1: Builder
FROM python:3.12-slim as builder
# Install build dependencies
RUN apt-get update && apt-get install -y \
gcc \
g++ \
libpq-dev \
python3-dev \
curl \
&& rm -rf /var/lib/apt/lists/*
# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python3 -
ENV PATH="/root/.local/bin:$PATH"
# Set working directory
WORKDIR /build
# Copy poetry files first for better caching
COPY pyproject.toml poetry.lock* ./
# Configure poetry to not create virtual environment
RUN poetry config virtualenvs.create false
# Export requirements for final stage
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes --without dev
# Stage 2: Runtime
FROM python:3.12-slim
# Install runtime dependencies and security updates
RUN apt-get update && apt-get install -y \
libpq5 \
curl \
gcc \
g++ \
libpq-dev \
python3-dev \
&& apt-get upgrade -y \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user with specific UID/GID for consistency
RUN groupadd -r -g 1000 cae && \
useradd -r -u 1000 -g cae -m -s /bin/bash cae
# Set working directory
WORKDIR /app
# Copy application code with proper ownership
COPY --from=builder --chown=cae:cae /build/requirements.txt .
COPY --chown=cae:cae app/ ./app/
COPY --chown=cae:cae migrations/ ./migrations/
# Install dependencies
RUN pip install --upgrade pip && \
pip install --no-cache-dir -r requirements.txt && \
# Clean up build dependencies
apt-get purge -y gcc g++ python3-dev && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /root/.cache/pip
# Security: Remove unnecessary packages and files
RUN apt-get purge -y --auto-remove curl && \
rm -rf /var/lib/apt/lists/* && \
# Remove any setuid/setgid files
find / -perm /6000 -type f -exec chmod a-s {} \; 2>/dev/null || true
# Create logs directory with proper permissions
RUN mkdir -p /app/logs && chown -R cae:cae /app/logs
# Switch to non-root user
USER cae
# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONPATH=/app
ENV PATH="/home/cae/.local/bin:${PATH}"
# Expose port
EXPOSE 8000
# Health check with tighter timing for production
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
# Run with production settings (no reload, higher worker count)
CMD ["uvicorn", "app.main:app", \
"--host", "0.0.0.0", \
"--port", "8000", \
"--workers", "4", \
"--loop", "uvloop", \
"--log-level", "warning", \
"--access-log"]