feat: Tối ưu Dockerfile của iam-service bằng cách lọc cài đặt pnpm, tinh chỉnh loại bỏ dev dependencies và thiết lập workspace đúng cách.

This commit is contained in:
Ho Ngoc Hai
2026-01-04 11:24:43 +07:00
parent c05ce82c7b
commit fcbe176b3e

View File

@@ -39,19 +39,27 @@ USER node
# VI: Copy source code (files phải thuộc về node user để pnpm có thể ghi)
COPY --chown=node:node . .
# EN: Build iam-service and its dependencies only
# VI: Chỉ build iam-service và các dependencies của nó
RUN pnpm install --frozen-lockfile=false
# EN: Install dependencies for iam-service only
# VI: Chỉ cài đặt dependencies cho iam-service
RUN pnpm install --frozen-lockfile=false --filter @goodgo/iam-service...
# EN: Generate Prisma Client explicitly
# VI: Generate Prisma Client một cách rõ ràng
RUN cd services/iam-service && npx prisma generate
# EN: Build all packages that iam-service depends on
# VI: Build tất cả packages mà iam-service phụ thuộc
RUN pnpm --filter @goodgo/iam-service... build
# EN: Prune dev dependencies after build
# VI: Prune dev dependencies sau khi build
RUN pnpm prune --prod
# EN: Manually remove dev dependencies to reduce image size but keep workspace packages
# VI: Manually xóa dev dependencies để giảm image size nhưng giữ workspace packages
RUN rm -rf services/iam-service/node_modules/.pnpm/*-dev-* && \
rm -rf node_modules/.pnpm/*@types+* && \
rm -rf node_modules/.pnpm/*eslint* && \
rm -rf node_modules/.pnpm/*jest* && \
rm -rf node_modules/.pnpm/*typescript* && \
rm -rf node_modules/.pnpm/*ts-* && \
rm -rf node_modules/.pnpm/*prettier*
# EN: Production stage - minimal runtime image
# VI: Production stage - minimal runtime image
@@ -71,33 +79,28 @@ RUN addgroup -g 1001 -S nodejs && \
# EN: Create necessary directories with correct permissions
# VI: Tạo necessary directories với permissions đúng
RUN mkdir -p /app/dist /app/node_modules /app/prisma && \
RUN mkdir -p /app && \
chown -R microservice:nodejs /app
# EN: Switch to non-root user
# VI: Switch sang non-root user
USER microservice
# EN: Copy built application from builder stage
# VI: Copy built application từ builder stage
COPY --from=deps --chown=microservice:nodejs /app/services/iam-service/dist ./dist
COPY --from=deps --chown=microservice:nodejs /app/node_modules ./node_modules
COPY --from=deps --chown=microservice:nodejs /app/services/iam-service/package.json ./
COPY --from=deps --chown=microservice:nodejs /app/services/iam-service/prisma ./prisma
# EN: Copy entire workspace structure with symlinks intact
# VI: Copy toàn bộ workspace structure với symlinks nguyên vẹn
COPY --from=deps --chown=microservice:nodejs /app /app
WORKDIR /app/services/iam-service
# EN: Add health check
# VI: Thêm health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:5000/health/live || exit 1
# EN: Expose port
# VI: Expose port
EXPOSE 5000
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget -qO- http://localhost:5001/health/live || exit 1
# EN: Use dumb-init to handle signals properly
# VI: Sử dụng dumb-init để handle signals properly
ENTRYPOINT ["dumb-init", "--"]
# EN: Start application
# VI: Start application
# EN: Start the service from the correct location
# VI: Khởi động service từ đúng vị trí
CMD ["node", "dist/main.js"]