Skip to content

Commit 6b5ba22

Browse files
author
VINGROUP\pn.tund2
committed
feat: implement optimized multi-stage Dockerfiles and refine docker-compose volume mounts for improved development performance
1 parent 2c7cafd commit 6b5ba22

4 files changed

Lines changed: 41 additions & 22 deletions

File tree

docker-compose.yml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ services:
2323
networks:
2424
- tatoo-network
2525
volumes:
26-
- ./strapi:/opt/app/strapi
27-
- /opt/app/strapi/node_modules
26+
# Mount only source code directories for hot-reload ✅
27+
# node_modules stays inside the container (Linux-compiled)
28+
- ./strapi/src:/opt/app/strapi/src
29+
- ./strapi/config:/opt/app/strapi/config
30+
- ./strapi/public:/opt/app/strapi/public
31+
- ./strapi/database:/opt/app/strapi/database
32+
- strapi-uploads:/opt/app/strapi/public/uploads
2833

2934
# Next.js Frontend Service
3035
next-app:
@@ -43,9 +48,14 @@ services:
4348
networks:
4449
- tatoo-network
4550
volumes:
46-
- ./next:/app/next
47-
- /app/next/node_modules
48-
- /app/next/.next
51+
# Mount only source code directories for hot-reload ✅
52+
# node_modules and .next cache stay inside the container
53+
- ./next/app:/app/next/app
54+
- ./next/components:/app/next/components
55+
- ./next/lib:/app/next/lib
56+
- ./next/public:/app/next/public
57+
- ./next/context:/app/next/context
58+
- ./next/types:/app/next/types
4959

5060
# PostgreSQL Database Service
5161
strapi-db:
@@ -89,6 +99,8 @@ services:
8999
volumes:
90100
strapi-db-data:
91101
driver: local
102+
strapi-uploads:
103+
driver: local
92104

93105
networks:
94106
tatoo-network:

next/Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ RUN apk add --no-cache --virtual .build-deps \
1414
g++ \
1515
libc6-compat
1616

17-
# Install pnpm globally
18-
RUN npm install -g pnpm@10
17+
# Install pnpm globally in builder
18+
RUN npm install -g pnpm@10 --no-update-notifier
1919

2020
WORKDIR /build
2121

@@ -43,16 +43,16 @@ RUN apk add --no-cache \
4343
curl \
4444
libc6-compat
4545

46-
# Enable corepack to provide pnpm in runtime ✅
47-
RUN corepack enable && corepack prepare pnpm@10.0.0 --activate
46+
# Install pnpm in runtime stage via npm (most reliable method)
47+
RUN npm install -g pnpm@10 --no-update-notifier
4848

4949
# Create non-root user ✅
5050
RUN addgroup -g 1001 -S nodejs && \
5151
adduser -S nodejs -u 1001
5252

5353
WORKDIR /app
5454

55-
# Copy build artifacts from builder
55+
# Copy entire build workspace (node_modules at root level for pnpm workspace)
5656
COPY --from=builder --chown=nodejs:nodejs /build .
5757

5858
# Ensure proper permissions ✅
@@ -67,5 +67,5 @@ EXPOSE 3000
6767
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
6868
CMD curl -f http://localhost:3000 || exit 1
6969

70-
# Start Next.js dev server
70+
# Start Next.js dev server from workspace root so pnpm resolves correctly
7171
CMD ["pnpm", "-C", "next", "dev"]

strapi/Dockerfile

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ RUN apk add --no-cache --virtual .build-deps \
1919
nasm \
2020
vips-dev
2121

22-
# Install pnpm globally
22+
# Install pnpm globally in builder
2323
RUN npm install -g pnpm@10
2424

2525
WORKDIR /build
@@ -29,8 +29,10 @@ COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
2929
COPY strapi/package.json ./strapi/
3030
COPY next/package.json ./next/
3131

32+
# Copy scripts folder BEFORE install as strapi postinstall depends on it ✅
33+
COPY strapi/scripts ./strapi/scripts
34+
3235
# Step 2: Install dependencies with frozen lockfile (consistency) ✅
33-
# Note: Postinstall scripts may fail in Docker, this is handled gracefully
3436
RUN pnpm install --frozen-lockfile
3537

3638
# Step 3: Copy remaining source code
@@ -51,33 +53,33 @@ RUN apk add --no-cache \
5153
bash \
5254
vips
5355

54-
# Enable corepack to provide pnpm in runtime ✅
55-
RUN corepack enable && corepack prepare pnpm@10.0.0 --activate
56+
# Install pnpm in runtime stage via npm (most reliable method)
57+
RUN npm install -g pnpm@10 --no-update-notifier
5658

5759
# Create non-root user for security ✅
5860
RUN addgroup -g 1001 -S nodejs && \
5961
adduser -S nodejs -u 1001
6062

6163
WORKDIR /opt/app
6264

63-
# Copy from builder stage - node_modules already compiled
65+
# Copy entire build workspace (node_modules at root level for pnpm workspace)
6466
COPY --from=builder --chown=nodejs:nodejs /build .
6567

66-
# Set working directory to strapi
67-
WORKDIR /opt/app/strapi
68-
6968
# Ensure proper permissions ✅
7069
RUN chown -R nodejs:nodejs /opt/app
7170

7271
# Switch to non-root user ✅
7372
USER nodejs
7473

74+
# Set working directory to strapi for CMD
75+
WORKDIR /opt/app/strapi
76+
7577
# Expose port
7678
EXPOSE 1337
7779

7880
# Health check ✅
79-
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
80-
CMD curl -f http://localhost:1337/admin || exit 1
81+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=5 \
82+
CMD curl -f http://localhost:1337/_health || exit 1
8183

82-
# Start Strapi
84+
# Start Strapi (runs in /opt/app/strapi, resolves node_modules from /opt/app/node_modules via workspace)
8385
CMD ["pnpm", "develop"]

test.Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM node:22-alpine
2+
RUN corepack enable && corepack prepare pnpm@10.0.0 --activate
3+
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001
4+
USER nodejs
5+
RUN command -v pnpm

0 commit comments

Comments
 (0)