-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
70 lines (52 loc) · 2.03 KB
/
Dockerfile
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
# ---- BUILD STAGE ----
FROM node:21-alpine3.18 AS builder
WORKDIR /app
# Copy only dependency files first for caching
COPY package.json pnpm-lock.yaml ./
RUN corepack enable && corepack prepare pnpm@latest --activate
# Install base build dependencies
RUN apk add --no-cache --virtual .gyp-build-deps \
python3 \
make \
g++ \
&& apk add --no-cache git
# Install packages (including devDependencies for building)
RUN pnpm install
# Now copy rest of source
COPY . .
# Build the project
RUN pnpm run build
# Remove dev deps and prune to production-only
RUN pnpm prune --prod
# Optional: clean up caches
RUN pnpm store prune && rm -rf /root/.npm
# ---- DEPLOY STAGE ----
FROM node:21-alpine3.18 AS deploy
# Create non-root user and group FIRST
RUN addgroup -g 1001 -S nodejs \
&& adduser -S -u 1001 nodejs -h /home/nodejs
# Ensure /app is owned by nodejs (before WORKDIR)
RUN mkdir -p /app && chown nodejs:nodejs /app
WORKDIR /app
ARG PORT
ENV PORT=$PORT
EXPOSE $PORT
# Copy files with ownership set to nodejs:nodejs
COPY --from=builder --chown=nodejs:nodejs /app/dist ./dist
COPY --from=builder --chown=nodejs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nodejs:nodejs /app/package.json ./
COPY --from=builder --chown=nodejs:nodejs /app/assets ./assets
# 1. Setup corepack AS ROOT
RUN corepack enable && \
corepack prepare pnpm@latest --activate
# 2. Switch to non-root user
USER nodejs
ENV PNPM_HOME=/home/nodejs/.pnpm-global
ENV PATH="$PNPM_HOME/bin:$PATH"
# 3. Create user's pnpm directories and configure
RUN mkdir -p $PNPM_HOME/bin && \
pnpm config set global-bin-dir $PNPM_HOME/bin
# 4. Install PM2 globally in user space
RUN pnpm install pm2 -g
# Final command remains the same
CMD ["pm2-runtime", "start", "dist/app.js", "--cron", "0 */12 * * *"]