-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathDockerfile
More file actions
62 lines (47 loc) · 2.49 KB
/
Copy pathDockerfile
File metadata and controls
62 lines (47 loc) · 2.49 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
# Development
# docker build --no-cache -t filesync --load .
# docker build --no-cache --platform linux/amd64 -t filesync --output=type=docker,dest=filesync.tar .
# ==============================
# Stage 1: Build Python dependencies
# ==============================
FROM python:3.14-alpine AS builder
# Install build dependencies
RUN apk add --no-cache --virtual .build-deps build-base libffi-dev musl-dev python3-dev
WORKDIR /filesync
# Copy requirements
COPY api/requirements.txt .
# Install Python dependencies to a separate location
RUN pip install --no-cache-dir --prefer-binary --upgrade pip setuptools wheel \
&& pip install --no-cache-dir --prefix=/install -r requirements.txt
# Optional: remove tests and unnecessary files from site-packages
RUN find /install/lib/ -path "*/site-packages/tests" -type d -exec rm -rf {} + \
&& find /install/lib/ -name "*.pyc" -delete \
&& find /install/lib/ -name "*.pyo" -delete \
&& find /install/lib/ -name "*.so" -exec strip --strip-unneeded {} +
# ==============================
# Stage 2: Runtime image
# ==============================
FROM python:3.14-alpine
# Install Nginx runtime (+ bash for a reliable `wait -n` in the start command)
RUN apk add --no-cache nginx bash
WORKDIR /filesync
# Copy Python dependencies from builder
COPY --from=builder /install /usr/local
# Copy app code
COPY api /filesync/api
COPY --chown=nginx:nginx web /filesync/web
# Copy Nginx config
COPY nginx.conf /etc/nginx/http.d/default.conf
# Health check: verify nginx (/) and backend (/health) are both responding
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD wget -qO /dev/null http://127.0.0.1/ && wget -qO /dev/null http://127.0.0.1:8000/health
# Expose ports
EXPOSE 80
# Start FastAPI + Nginx. bash's `wait -n` returns as soon as EITHER process exits, so the
# container stops (and `restart: unless-stopped` restarts it) if the backend dies —
# instead of nginx keeping a half-dead container alive indefinitely. (busybox sh's
# `wait -n` does not reliably fire for a backgrounded child as PID 1, so we use bash.)
# --ws-max-size caps WebSocket frames at the transport (uvicorn's default is 16 MiB,
# which would be fully buffered before the app's own 32 KiB payload check runs);
# 64 KiB leaves headroom over the signaling _MAX_PAYLOAD_BYTES limit.
CMD ["bash", "-c", "python3 -m uvicorn api.main:app --host 0.0.0.0 --port 8000 --ws-max-size 65536 & nginx -g 'daemon off;' & wait -n"]