build(docker): build frontend and server inside container; remove host build deps

This commit is contained in:
Chenwei Jiang 2025-08-21 16:47:05 +08:00
parent 11104dbf29
commit 7591f001c1
Signed by: cheverjohn
GPG key ID: ADC4815BFE960182
6 changed files with 75 additions and 43 deletions

View file

@ -1,4 +1,21 @@
FROM node:20-bullseye-slim
######
# Multi-stage production image that builds EVERYTHING inside containers
######
# ---------- Frontend builder ----------
FROM node:20-bullseye-slim AS frontend-builder
ENV npm_config_registry=https://registry.npmmirror.com
WORKDIR /app
# Install frontend deps and build (root project)
COPY package.json ./
COPY pnpm-lock.yaml ./
RUN npm install --no-audit --no-fund
COPY . .
RUN npm run build
# ---------- Server builder ----------
FROM node:20-bullseye-slim AS server-builder
ENV npm_config_registry=https://registry.npmmirror.com
ENV npm_config_build_from_source=true
@ -7,19 +24,31 @@ RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 make g++ \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app/server
COPY server/package.json ./package.json
RUN npm install --no-audit --no-fund --build-from-source
COPY server/tsconfig.json ./tsconfig.json
COPY server/src ./src
RUN npm run build
# Prune dev deps after build; keep native modules compiled for runtime
RUN npm prune --omit=dev
# ---------- Runtime ----------
FROM node:20-bullseye-slim
ENV npm_config_registry=https://registry.npmmirror.com
WORKDIR /app
# Install server deps in container (native modules match linux/amd64)
COPY server/package.json ./server/package.json
RUN cd server && npm install --omit=dev --build-from-source
# Copy server runtime and prebuilt frontend from host
COPY server/dist ./server/dist
COPY dist ./frontend
ENV PORT=3004
ENV DB_PATH=/data/purchase.db
# Server runtime
COPY --from=server-builder /app/server/dist ./server/dist
COPY --from=server-builder /app/server/node_modules ./server/node_modules
COPY server/package.json ./server/package.json
# Prebuilt frontend (from frontend-builder)
COPY --from=frontend-builder /app/dist ./frontend
EXPOSE 3004
CMD ["node", "server/dist/index.js"]