Skip to content

Commit b1ab0ce

Browse files
author
VINGROUP\pn.tund2
committed
feat: add Dockerfiles for Next.js and Strapi services with multi-stage builds
1 parent 206a3dc commit b1ab0ce

3 files changed

Lines changed: 87 additions & 127 deletions

File tree

next/Dockerfile

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,62 @@
1-
# ============================================================================
1+
# ==========================================
22
# NEXT.JS DOCKERFILE - DEVELOPMENT (Optimized)
3-
# ============================================================================
4-
# Multi-stage build for efficient development image
5-
# Image size: ~200MB (vs 500MB before)
6-
# Build time: Optimized with cache-friendly layer ordering
3+
# ==========================================
4+
# Multi-stage development build for optimized speed and caching
75

8-
FROM node:22-alpine AS builder
6+
# STAGE 1: Build
7+
FROM node:22-alpine AS build
98

10-
# Install build dependencies in builder only ✅
11-
RUN apk add --no-cache --virtual .build-deps \
12-
python3 \
13-
make \
14-
g++ \
15-
libc6-compat
9+
# Install build dependencies
10+
RUN apk add --no-cache build-base gcc autoconf automake libc6-compat python3 > /dev/null 2>&1
1611

17-
# Install pnpm globally in builder
18-
RUN npm install -g pnpm@10 --no-update-notifier
12+
WORKDIR /opt/
1913

20-
WORKDIR /build
14+
# 1. Bật Corepack để kích hoạt pnpm ✅
15+
RUN corepack enable && corepack prepare pnpm@10.0.0 --activate
2116

22-
# Step 1: Copy dependency files first (cache optimization)
17+
# 2. Copy workspace config và package files
2318
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
2419
COPY next/package.json ./next/
2520
COPY strapi/package.json ./strapi/
2621

27-
# Step 2: Install with frozen lockfile (consistency)
22+
# 3. Cài đặt dependencies cho toàn bộ workspace
2823
RUN pnpm install --frozen-lockfile
2924

30-
# Step 3: Copy source code
31-
COPY next ./next
25+
# 4. Copy source code và build dự án (nếu cần thiết cho dev assets) ✅
26+
COPY . .
27+
# Cho Next.js dev, build không bắt buộc nhưng giúp verify config
28+
RUN pnpm run build --filter nextjs
3229

33-
# ============================================================================
34-
# RUNTIME STAGE - Minimal and secure
35-
# ============================================================================
30+
# STAGE 2: Runtime
3631
FROM node:22-alpine
3732

38-
LABEL maintainer="DevOps Team"
39-
LABEL description="Next.js Frontend - Development"
33+
# Install runtime dependencies
34+
RUN apk add --no-cache libc6-compat curl bash > /dev/null 2>&1
4035

41-
# Install only runtime dependencies ✅
42-
RUN apk add --no-cache \
43-
curl \
44-
libc6-compat
36+
ARG NODE_ENV=development
37+
ENV NODE_ENV=${NODE_ENV}
4538

46-
# Install pnpm in runtime stage via npm (most reliable method) ✅
47-
RUN npm install -g pnpm@10 --no-update-notifier
39+
WORKDIR /opt/
4840

49-
# Create non-root user ✅
50-
RUN addgroup -g 1001 -S nodejs && \
51-
adduser -S nodejs -u 1001
41+
# 5. Bật Corepack ở stage runtime ✅
42+
RUN corepack enable && corepack prepare pnpm@10.0.0 --activate
5243

53-
WORKDIR /app
44+
# Copy dependencies and application logic from build stage ✅
45+
COPY --from=build /opt/node_modules /opt/node_modules
46+
COPY --from=build /opt/package.json /opt/pnpm-lock.yaml /opt/pnpm-workspace.yaml /opt/
5447

55-
# Copy entire build workspace (node_modules at root level for pnpm workspace) ✅
56-
COPY --from=builder --chown=nodejs:nodejs /build .
48+
WORKDIR /opt/app
49+
COPY --from=build /opt/app ./
5750

58-
# Ensure proper permissions
59-
RUN chown -R nodejs:nodejs /app
51+
# 6. Set PATH correctly
52+
ENV PATH=/opt/node_modules/.bin:$PATH
6053

61-
# Switch to non-root user ✅
62-
USER nodejs
54+
# Ensure non-root node user
55+
RUN chown -R node:node /opt/ /opt/app
56+
USER node
6357

6458
EXPOSE 3000
6559

66-
# Health check ✅
67-
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
68-
CMD curl -f http://localhost:3000 || exit 1
69-
70-
# Start Next.js dev server from workspace root so pnpm resolves correctly
60+
# 7. Start Next.js in development mode
61+
# Note: we use -C next to run pnpm within the next directory
7162
CMD ["pnpm", "-C", "next", "dev"]

next/Dockerfile.prod

Lines changed: 43 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,67 @@
1-
# ============================================================================
1+
# ==========================================
22
# NEXT.JS DOCKERFILE.PROD - PRODUCTION (Highly Optimized)
3-
# ============================================================================
3+
# ==========================================
44
# Multi-stage production build for minimal size and best performance
5-
# Image size: ~150MB (vs 500MB unoptimized)
6-
# Build time: ~3-5 minutes with proper layer caching
7-
# Security: Non-root user, immutable files, minimal dependencies
85

9-
ARG NODE_VERSION=22-alpine
6+
# STAGE 1: Build
7+
FROM node:22-alpine AS build
108

11-
# ============================================================================
12-
# STAGE 1: Dependencies Builder
13-
# ============================================================================
14-
FROM node:${NODE_VERSION} AS deps
9+
# Install build dependencies
10+
RUN apk add --no-cache build-base gcc autoconf automake libc6-compat python3 > /dev/null 2>&1
1511

16-
RUN npm install -g pnpm@10 --no-save
12+
WORKDIR /opt/
1713

18-
WORKDIR /build
14+
# 1. Bật Corepack để kích hoạt pnpm ✅
15+
RUN corepack enable && corepack prepare pnpm@10.0.0 --activate
1916

20-
# Copy dependency files (cache-optimized)
17+
# 2. Copy workspace config và package files
2118
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
2219
COPY next/package.json ./next/
2320
COPY strapi/package.json ./strapi/
2421

25-
# Install all dependencies first
22+
# 3. Cài đặt toàn bộ dependencies (bao gồm dev deps để build) ✅
2623
RUN pnpm install --frozen-lockfile
2724

28-
# ============================================================================
29-
# STAGE 2: Builder - Build Next.js ✅
30-
# ============================================================================
31-
FROM node:${NODE_VERSION} AS builder
32-
33-
RUN npm install -g pnpm@10 --no-save && \
34-
apk add --no-cache --virtual .build-deps \
35-
python3 \
36-
make \
37-
g++ \
38-
libc6-compat
39-
40-
WORKDIR /build
41-
42-
# Copy deps from stage 1
43-
COPY --from=deps /build/node_modules ./node_modules
44-
COPY --from=deps /build/package.json .
45-
COPY --from=deps /build/pnpm-lock.yaml .
46-
COPY --from=deps /build/pnpm-workspace.yaml .
47-
48-
# Copy source
49-
COPY next ./next
50-
51-
# Build Next.js app
52-
RUN pnpm -C next build
53-
54-
# ============================================================================
55-
# STAGE 3: Runtime - Minimal production image ✅
56-
# ============================================================================
57-
FROM node:${NODE_VERSION}
58-
59-
LABEL maintainer="DevOps Team"
60-
LABEL version="1.0"
61-
LABEL description="Next.js Frontend - Production Optimized"
62-
63-
# Install runtime dependencies only
64-
RUN apk add --no-cache \
65-
curl \
66-
libc6-compat \
67-
ca-certificates && \
68-
rm -rf /var/cache/apk/*
69-
70-
# Enable corepack to provide pnpm in runtime ✅
71-
RUN corepack enable && corepack prepare pnpm@10.0.0 --activate
25+
# 4. Copy source code và build dự án ✅
26+
COPY . .
27+
RUN pnpm run build --filter nextjs
28+
29+
# 5. Prune dev dependencies cho production mode ✅
30+
RUN pnpm prune --prod
31+
32+
# ==========================================
33+
# STAGE 2: Production Runtime
34+
# ==========================================
35+
FROM node:22-alpine
7236

73-
# Create non-root user
74-
RUN addgroup -g 1001 -S nodejs && \
75-
adduser -S nodejs -u 1001 && \
76-
mkdir -p /app && \
77-
chown -R nodejs:nodejs /app
37+
# Install minimal runtime dependencies
38+
RUN apk add --no-cache libc6-compat curl bash ca-certificates > /dev/null 2>&1
7839

79-
WORKDIR /app
40+
ARG NODE_ENV=production
41+
ENV NODE_ENV=${NODE_ENV}
8042

81-
# Copy built Next.js app
82-
COPY --from=builder --chown=nodejs:nodejs /build/node_modules ./node_modules
83-
COPY --from=builder --chown=nodejs:nodejs /build/package.json .
84-
COPY --from=builder --chown=nodejs:nodejs /build/pnpm-lock.yaml .
85-
COPY --from=builder --chown=nodejs:nodejs /build/next ./next
43+
WORKDIR /opt/
8644

45+
# 6. Bật Corepack ở stage runtime để chạy pnpm ✅
46+
RUN corepack enable && corepack prepare pnpm@10.0.0 --activate
47+
48+
# 7. Copy các file tối thiểu từ build stage ✅
49+
# Chỉ copy node_modules đã prune và build artifacts
50+
COPY --from=build /opt/node_modules ./node_modules
51+
COPY --from=build /opt/package.json /opt/pnpm-lock.yaml /opt/pnpm-workspace.yaml /opt/
52+
COPY --from=build /opt/next ./next
53+
54+
WORKDIR /opt/app
8755
# Set environment variables
8856
ENV NODE_ENV=production
8957
ENV NEXT_TELEMETRY_DISABLED=1
58+
ENV PATH=/opt/node_modules/.bin:$PATH
9059

91-
# Switch to non-root user
92-
USER nodejs
60+
# Ensure non-root node user
61+
RUN chown -R node:node /opt/ /opt/app
62+
USER node
9363

9464
EXPOSE 3000
9565

96-
# Health check
97-
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
98-
CMD curl -f http://localhost:3000 || exit 1
99-
100-
# Start production server
101-
CMD ["pnpm", "-C", "next", "start"]
66+
# 8. Start production server
67+
CMD ["pnpm", "-C", "next", "run", "start"]

strapi/Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ COPY strapi/scripts ./strapi/scripts
2525
# 3. Cài đặt dependencies cho toàn bộ workspace ✅
2626
RUN pnpm install --frozen-lockfile
2727

28+
# 4. Copy source code và build dự án (giống mẫu của bạn) ✅
29+
COPY . .
30+
RUN pnpm run build --filter strapi
31+
2832
# STAGE 2: Runtime
2933
FROM node:22-alpine
3034

@@ -39,13 +43,12 @@ WORKDIR /opt/
3943
# 4. Bật Corepack ở stage runtime ✅
4044
RUN corepack enable && corepack prepare pnpm@10.0.0 --activate
4145

42-
# Copy dependencies from build stage ✅
43-
COPY --from=build /opt/node_modules ./node_modules
44-
COPY --from=build /opt/package.json /opt/pnpm-lock.yaml /opt/pnpm-workspace.yaml ./
46+
# Copy dependencies and application logic from build stage ✅
47+
COPY --from=build /opt/node_modules /opt/node_modules
48+
COPY --from=build /opt/package.json /opt/pnpm-lock.yaml /opt/pnpm-workspace.yaml /opt/
4549

4650
WORKDIR /opt/app
47-
# Copy source code (will be partially overridden by volumes in compose)
48-
COPY . .
51+
COPY --from=build /opt/app ./
4952

5053
# 5. Set PATH correctly ✅
5154
ENV PATH=/opt/node_modules/.bin:$PATH

0 commit comments

Comments
 (0)