-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
54 lines (39 loc) · 1.4 KB
/
Copy pathDockerfile
File metadata and controls
54 lines (39 loc) · 1.4 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
# Multi-stage build for smaller final image
FROM python:3.11-slim AS builder
WORKDIR /build
# Install build dependencies and copy requirements
COPY requirements.txt .
# Install dependencies to user directory for easy copying
RUN pip install --user --no-cache-dir -r requirements.txt
# --- Final Stage ---
FROM python:3.11-slim
# Create non-root user for security
RUN useradd --create-home --shell /bin/bash --uid 1000 appuser
WORKDIR /app
# Copy installed packages from builder
COPY --from=builder /root/.local /home/appuser/.local
# Copy application code
COPY --chown=appuser:appuser app/ ./app/
COPY --chown=appuser:appuser requirements.txt .
# Create runtime directories with correct permissions
RUN mkdir -p /app/data /app/models && \
chown -R appuser:appuser /app
# Switch to non-root user
USER appuser
# Add local bin to PATH for installed packages
ENV PATH=/home/appuser/.local/bin:$PATH
ENV PYTHONPATH=/app
ENV PYTHONUNBUFFERED=1
# Install curl for healthcheck (as root, then switch back)
USER root
RUN apt-get update && apt-get install -y --no-install-recommends curl && \
rm -rf /var/lib/apt/lists/* && \
apt-get clean
USER appuser
# Expose port
EXPOSE 8000
# Healthcheck
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Run the application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]