-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathDockerfile
More file actions
103 lines (72 loc) · 2.94 KB
/
Copy pathDockerfile
File metadata and controls
103 lines (72 loc) · 2.94 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
100
101
102
103
# syntax=docker/dockerfile:1
# --- Frontend Build Stage ---
FROM node:20-slim AS frontend-builder
WORKDIR /app/frontend
# Copy package manifests
COPY frontend/package.json frontend/package-lock.json* ./
# Install dependencies
RUN npm install --production
# Copy frontend source
COPY frontend/ ./
# Copy version.txt before build so it can be embedded into the frontend
COPY version.txt ./
# Build the frontend
RUN BUILD_MODE=static npm run build
# --- Backend Build Stage ---
FROM python:3.11-slim AS backend-builder
WORKDIR /app/backend
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
pkg-config \
libffi-dev \
default-libmysqlclient-dev \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Install uv
RUN pip install uv
# Copy backend dependency files
COPY backend/pyproject.toml backend/uv.lock ./
# Install backend dependencies
RUN uv pip install --system --no-cache -r pyproject.toml
# Copy backend source
COPY backend/ ./
# --- Final Image Stage ---
FROM python:3.11-slim
WORKDIR /app
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Disable in-app migrations because the entrypoint handles them
ENV RUN_MIGRATIONS_IN_APP=false
# Install uv, cron, and nginx in the final image
RUN pip install uv && apt-get update && apt-get install -y cron nginx && rm -rf /var/lib/apt/lists/*
# Copy installed dependencies from backend-builder
COPY --from=backend-builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=backend-builder /usr/local/bin /usr/local/bin
# Copy backend application
COPY --from=backend-builder /app/backend /app
# Copy the generated .env file for production
COPY .env /app/.env
# Copy built frontend to the static directory
# The FastAPI app must be configured to serve static files from this directory.
COPY --from=frontend-builder /app/frontend/dist /app/static
# Copy version.txt so the backend can read the installed version
COPY version.txt /app/version.txt
# Copy backup script
COPY backend/backup_db.sh /app/backup_db.sh
RUN chmod +x /app/backup_db.sh
# Copy nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf
# Expose the port the app runs on
EXPOSE 8000
# Health check using the /health endpoint
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
# Copy entrypoint script and make it executable
COPY backend/docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# Set the entrypoint
ENTRYPOINT ["docker-entrypoint.sh"]
# The command to run the application with Gunicorn and multiple Uvicorn workers
# nginx (port 8000) -> Gunicorn (port 8001) for static file performance
CMD ["gunicorn", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "--bind", "127.0.0.1:8001", "--timeout", "120", "--keep-alive", "5", "--access-logfile", "-", "app.main:app"]