refactor: Optimize Docker build process, switch to @node-rs/bcrypt, and streamline compose.yaml by removing external services.
All checks were successful
Build and Deploy Backend / Build Backend Docker Image (push) Successful in 45s
Build and Deploy Backend / Deploy E-learning Backend to Dev Server (push) Successful in 12s
Build and Deploy Backend / Notify Deployment Status (push) Successful in 1s

This commit is contained in:
JakkrapartXD 2026-02-10 15:26:39 +07:00
parent 18660fa364
commit c1254520b4
6 changed files with 218 additions and 405 deletions

View file

@ -1,35 +1,14 @@
# Build stage
FROM node:20-alpine AS builder
FROM node:20-alpine
WORKDIR /app
# Install build dependencies for native modules (bcrypt, prisma)
RUN apk add --no-cache python3 make g++
# Copy package files
COPY package*.json ./
COPY pnpm-lock.yaml* ./
# Install pnpm and dependencies with build scripts enabled
RUN npm install -g pnpm && \
pnpm install --frozen-lockfile --ignore-scripts=false
# Copy source code
COPY . .
# Generate Prisma client
RUN pnpm prisma:generate
# Build application
RUN pnpm build
# Production stage
FROM node:20-alpine AS production
WORKDIR /app
# Install build dependencies for native modules (bcrypt, prisma)
RUN apk add --no-cache python3 make g++
# Install dependencies ทั้ง runtime และ build
RUN apk add --no-cache \
python3 \
make \
g++ \
openssl-dev \
libstdc++
# Install pnpm
RUN npm install -g pnpm
@ -38,27 +17,28 @@ RUN npm install -g pnpm
COPY package*.json ./
COPY pnpm-lock.yaml* ./
# Install production dependencies with build scripts enabled
RUN pnpm install --frozen-lockfile --ignore-scripts=false
# Install dependencies
RUN pnpm install --frozen-lockfile
# Copy prisma schema and generate client
COPY --from=builder /app/prisma ./prisma
# Copy prisma schema
COPY prisma ./prisma
# Generate Prisma client
RUN pnpm prisma:generate
# Copy built files from builder
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/public ./public
# Copy source code
COPY . .
# Build application
RUN pnpm build
# Set environment
ENV NODE_ENV=production
ENV PORT=4000
# Expose port
EXPOSE 4000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:4000/health || exit 1
# Start application
CMD ["node", "dist/server.js"]
CMD ["npm", "start"]