Skip to content

Commit f345c4d

Browse files
authored
fix(docker): updated docker to use non-root user for k8s/helm deployments (#1626)
* fix(docker): updated docker to use non-root user for k8s/helm deployments * ack PR comments
1 parent f147eae commit f345c4d

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

docker/app.Dockerfile

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,31 @@ RUN apk add --no-cache python3 py3-pip bash
6363

6464
ENV NODE_ENV=production
6565

66-
COPY --from=builder /app/apps/sim/public ./apps/sim/public
67-
COPY --from=builder /app/apps/sim/.next/standalone ./
68-
COPY --from=builder /app/apps/sim/.next/static ./apps/sim/.next/static
66+
# Create non-root user and group
67+
RUN addgroup -g 1001 -S nodejs && \
68+
adduser -S nextjs -u 1001
6969

70-
# Copy guardrails setup script and requirements
71-
COPY --from=builder /app/apps/sim/lib/guardrails/setup.sh ./apps/sim/lib/guardrails/setup.sh
72-
COPY --from=builder /app/apps/sim/lib/guardrails/requirements.txt ./apps/sim/lib/guardrails/requirements.txt
73-
COPY --from=builder /app/apps/sim/lib/guardrails/validate_pii.py ./apps/sim/lib/guardrails/validate_pii.py
70+
COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/public ./apps/sim/public
71+
COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/.next/standalone ./
72+
COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/.next/static ./apps/sim/.next/static
7473

75-
# Run guardrails setup to create venv and install Python dependencies
74+
# Guardrails setup (files need to be owned by nextjs for runtime)
75+
COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/lib/guardrails/setup.sh ./apps/sim/lib/guardrails/setup.sh
76+
COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/lib/guardrails/requirements.txt ./apps/sim/lib/guardrails/requirements.txt
77+
COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/lib/guardrails/validate_pii.py ./apps/sim/lib/guardrails/validate_pii.py
78+
79+
# Run guardrails setup as root, then fix ownership of generated venv files
7680
RUN chmod +x ./apps/sim/lib/guardrails/setup.sh && \
7781
cd ./apps/sim/lib/guardrails && \
78-
./setup.sh
82+
./setup.sh && \
83+
chown -R nextjs:nodejs /app/apps/sim/lib/guardrails
84+
85+
# Create .next/cache directory with correct ownership
86+
RUN mkdir -p apps/sim/.next/cache && \
87+
chown -R nextjs:nodejs /app
88+
89+
# Switch to non-root user
90+
USER nextjs
7991

8092
EXPOSE 3000
8193
ENV PORT=3000 \

docker/db.Dockerfile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ RUN bun install --ignore-scripts
1717
FROM oven/bun:1.2.22-alpine AS runner
1818
WORKDIR /app
1919

20+
# Create non-root user and group
21+
RUN addgroup -g 1001 -S nodejs && \
22+
adduser -S nextjs -u 1001
23+
2024
# Copy only the necessary files from deps
21-
COPY --from=deps /app/node_modules ./node_modules
22-
COPY packages/db/drizzle.config.ts ./packages/db/drizzle.config.ts
23-
COPY packages/db ./packages/db
25+
COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules
26+
COPY --chown=nextjs:nodejs packages/db/drizzle.config.ts ./packages/db/drizzle.config.ts
27+
COPY --chown=nextjs:nodejs packages/db ./packages/db
28+
29+
# Switch to non-root user
30+
USER nextjs
2431

2532
WORKDIR /app/packages/db

docker/realtime.Dockerfile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ WORKDIR /app
3636

3737
ENV NODE_ENV=production
3838

39+
# Create non-root user and group
40+
RUN addgroup -g 1001 -S nodejs && \
41+
adduser -S nextjs -u 1001
42+
3943
# Copy the sim app and the shared db package needed by socket-server
40-
COPY --from=builder /app/apps/sim ./apps/sim
41-
COPY --from=builder /app/packages/db ./packages/db
42-
COPY --from=builder /app/node_modules ./node_modules
43-
COPY --from=builder /app/package.json ./package.json
44+
COPY --from=builder --chown=nextjs:nodejs /app/apps/sim ./apps/sim
45+
COPY --from=builder --chown=nextjs:nodejs /app/packages/db ./packages/db
46+
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
47+
COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json
48+
49+
# Switch to non-root user
50+
USER nextjs
4451

4552
# Expose socket server port (default 3002, but configurable via PORT env var)
4653
EXPOSE 3002

0 commit comments

Comments
 (0)