FROM node:20-slim AS builder
# Install required tools
RUN apt-get update && \
apt-get install -y --no-install-recommends \
build-essential \
git \
ca-certificates \
python3 \
python3-venv \
patch && \
rm -rf /var/lib/apt/lists/*
WORKDIR /workspace
# Corepack and PNPM
RUN corepack enable && \
corepack prepare pnpm@9.15.0 --activate
# Copy repository
COPY . .
# Install dependencies for the server package only (to keep install fast and deterministic)
RUN pnpm install --no-frozen-lockfile --filter @counterscale/server
# Build the server package only to reduce work
RUN pnpm --filter @counterscale/server build
# Stage 2: runtime
FROM node:20-slim AS runner
# Create non-root user
RUN groupadd -r appuser && useradd -r -g appuser appuser
WORKDIR /app
# Copy built artifacts
COPY --from=builder /workspace/packages/server/build ./build
# Install static server (no runtime dependencies from the app)
RUN apt-get update && \
apt-get install -y --no-install-recommends ca-certificates && \
rm -rf /var/lib/apt/lists/*
RUN npm i -g serve
# Ensure permissions for the runtime user
RUN chown -R appuser:appuser /app
USER appuser
EXPOSE 8080
CMD ["serve", "-s", "build", "-l", "8080"]