Repository navigation
Expand file tree
/
Copy pathDockerfile.prod
More file actions
80 lines (64 loc) · 2.91 KB
/
Copy pathDockerfile.prod
File metadata and controls
80 lines (64 loc) · 2.91 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
# QStack for Cloud Run.
#
# The database is external now (Cloud SQL), so this image is one process: a
# gunicorn serving the API and the built frontend on $PORT. It previously ran
# PostgreSQL inside the container with a VOLUME for the data directory -- which
# Cloud Run ignores, since its filesystem is in-memory and discarded when the
# instance goes away -- and listened on a hardcoded 3001, which Cloud Run would
# never have routed to.
#
# Build: docker build -f Dockerfile.prod -t qstack .
# Run: docker run -e PORT=8080 -e MYSQL_... -p 8080:8080 qstack
# ------------------------------------------------------------- frontend build
FROM node:18-alpine AS frontend-builder
WORKDIR /app/client
COPY client/package*.json ./
RUN npm ci && npm cache clean --force
COPY client/ ./
RUN npm run build
# -------------------------------------------------------------- python build
FROM python:3.9-slim-bullseye AS build
# No apt layer: every dependency ships a wheel (psycopg2-binary and PyMySQL
# included), so there is nothing to compile. Installing build tools here also
# broke outright once Debian bullseye's security archive moved on, which is how
# this image came to fail a build it had previously passed.
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt gunicorn
# ------------------------------------------------------------ runtime image
FROM python:3.9-slim-bullseye
# psycopg2-binary bundles its own libpq, so there is no apt layer here either.
# No compiler, no database server, no startup shell script.
# Nothing here needs root.
RUN useradd --create-home --shell /usr/sbin/nologin qstack
WORKDIR /app
COPY --from=build /install /usr/local
COPY --chown=qstack:qstack server/ ./server/
COPY --chown=qstack:qstack wsgi.py gunicorn.conf.py ./
COPY --from=frontend-builder --chown=qstack:qstack /app/client/dist ./client/dist
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PORT=8080 \
DEBUG=false \
ENVIRONMENT=production \
AUTH_ENVIRONMENT=production \
MIN_ACCESS_ROLE=0 \
MIN_ADMIN_ROLE=1 \
AUTH_SERVER_URL=https://auth.hackpsu.org/api/sessionUser \
AUTH_LOGIN_URL=https://auth.hackpsu.org/login \
AUTH_LOGOUT_URL=https://auth.hackpsu.org/api/sessionLogout \
HACKPSU_API_URL=https://apiv3.hackpsu.org
# PORT Cloud Run injects this; gunicorn.conf.py binds it.
# FRONTEND_URL / BACKEND_URL / QSTACK_URL
# Set these to the service's own URL at deploy time -- they drive
# CORS and the auth redirect, so a stale value breaks login.
#
# Required at deploy time:
# APP_SECRET_KEY signs the session cookie
# MYSQL_USER / MYSQL_PASSWORD / MYSQL_DATABASE / MYSQL_SOCKET_PATH
# Cloud SQL, composed into a URI by server/config.py
USER qstack
EXPOSE 8080
# gunicorn is PID 1 and handles SIGTERM, which is how Cloud Run drains an
# instance.
CMD ["gunicorn", "--config", "gunicorn.conf.py", "wsgi:app"]