-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
53 lines (38 loc) · 1.52 KB
/
Copy pathDockerfile
File metadata and controls
53 lines (38 loc) · 1.52 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
# --- Stage 1: Build Stage ---
FROM python:3.11-slim AS builder
# Prevent Python from writing .pyc files and enable unbuffered logging
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /app
# Install build dependencies only (will be discarded in the final image)
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
# Install dependencies to a local directory
RUN pip install --no-cache-dir --user -r requirements.txt
# --- Stage 2: Final Runtime Stage ---
FROM python:3.11-slim AS runner
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PATH="/home/appuser/.local/bin:${PATH}"
WORKDIR /app
# 1. Create a non-privileged system user
RUN groupadd -g 10001 appgroup && \
useradd -u 10001 -g appgroup -s /bin/bash -m appuser
# 2. Install Tini (Init process to handle signals properly)
RUN apt-get update && apt-get install -y --no-install-recommends \
tini \
&& rm -rf /var/lib/apt/lists/*
# 3. Copy only necessary artifacts from builder
COPY --from=builder --chown=appuser:appgroup /root/.local /home/appuser/.local
COPY --chown=appuser:appgroup . .
# 4. Switch to the non-root user
USER 10001
# 5. Expose port (Documentation only)
EXPOSE 8000
# 6. Use tini as entrypoint to manage zombie processes and signals
ENTRYPOINT ["/usr/bin/tini", "--"]
# 7. Run uvicorn with security-minded flags
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--no-access-log", "--proxy-headers"]