-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.backend
More file actions
88 lines (70 loc) · 2.43 KB
/
Dockerfile.backend
File metadata and controls
88 lines (70 loc) · 2.43 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
# Build stage
FROM python:3.11.9-slim-bookworm AS builder
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_DEFAULT_TIMEOUT=100
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
curl \
&& rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /app
# Copy requirements first to leverage Docker cache
COPY backend/requirements.txt .
# Install Python dependencies
RUN pip install --upgrade pip && \
pip install --no-cache-dir --prefer-binary -r requirements.txt
# Final stage
FROM python:3.11.9-slim-bookworm
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
wget \
curl \
libpq5 \
libmagic1 \
tesseract-ocr \
poppler-utils \
&& rm -rf /var/lib/apt/lists/*
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PATH="/root/.local/bin:$PATH" \
# UID for the non-root user
APP_UID=1001 \
APP_GID=1001 \
APP_USER=appuser \
APP_HOME=/app
# Create non-root user and set up directories
RUN groupadd -g "${APP_GID}" "${APP_USER}" \
&& useradd --create-home --no-log-init -u "${APP_UID}" -g "${APP_GID}" "${APP_USER}" \
&& mkdir -p "${APP_HOME}" \
&& chown -R "${APP_USER}:${APP_GID}" "${APP_HOME}"
# Set working directory and switch to non-root user
WORKDIR ${APP_HOME}
# Copy installed packages from builder
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
# Switch to non-root user
USER ${APP_USER}
# Copy application code
COPY --chown=${APP_USER}:${APP_GID} backend/ ${APP_HOME}/
# Create application directory if it doesn't exist
RUN mkdir -p ${APP_HOME} \
&& chown -R ${APP_USER}:${APP_GID} ${APP_HOME}
# Install requirements
COPY --chown=${APP_USER}:${APP_GID} backend/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Expose the port the app runs on
EXPOSE 8000
# Command to run the application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]