refactor: consolidate Dockerfile stages and improve build process for frontend and backend

This commit is contained in:
Tudor Stanciu 2025-10-03 19:10:51 +03:00
parent ed46132f64
commit db095eb519

View File

@ -1,36 +1,32 @@
# Multi-stage Dockerfile for Bitip GeoIP Service # Multi-stage Dockerfile for Bitip GeoIP Service
# Stage 1: Build frontend # Stage 1: Build application
FROM node:24.9-alpine3.21 AS frontend-builder FROM node:24.9-alpine3.21 AS builder
WORKDIR /app/frontend WORKDIR /app
# Copy frontend package files # Copy workspace configuration
COPY src/frontend/package*.json ./ COPY package*.json ./
RUN npm ci --only=production
# Copy frontend source # Copy workspace package files
COPY src/frontend/ ./ COPY src/backend/package*.json ./src/backend/
COPY src/frontend/package*.json ./src/frontend/
# Build frontend # Install all dependencies (including devDependencies for build)
RUN npm run build
# Stage 2: Build backend
FROM node:24.9-alpine3.21 AS backend-builder
WORKDIR /app/backend
# Copy backend package files
COPY src/backend/package*.json ./
COPY src/backend/tsconfig.json ./
RUN npm ci RUN npm ci
# Copy backend source # Copy source code
COPY src/backend/ ./ COPY src/ ./src/
COPY tsconfig.json ./
COPY .gitattributes ./
# Build backend # Copy configuration files needed for build
COPY ReleaseNotes.json ./
COPY Overview.json ./
# Build both frontend and backend
RUN npm run build RUN npm run build
# Stage 3: Production image # Stage 2: Production image
FROM node:24.9-alpine3.21 FROM node:24.9-alpine3.21
# Build arguments for versioning # Build arguments for versioning
@ -51,10 +47,11 @@ LABEL org.opencontainers.image.authors="Tudor Stanciu <tudor.stanciu94@gmail.com
org.opencontainers.image.version="${APP_VERSION}" \ org.opencontainers.image.version="${APP_VERSION}" \
org.opencontainers.image.revision="${GIT_REVISION}" org.opencontainers.image.revision="${GIT_REVISION}"
# Set version environment variable (accessible at runtime) # Set version environment variables (accessible at runtime)
ENV APP_VERSION=${APP_VERSION} \ ENV APP_VERSION=${APP_VERSION} \
CREATED_AT=${CREATED_AT} \ CREATED_AT=${CREATED_AT} \
GIT_REVISION=${GIT_REVISION} GIT_REVISION=${GIT_REVISION} \
NODE_ENV=production
# Install dumb-init for proper signal handling # Install dumb-init for proper signal handling
RUN apk add --no-cache dumb-init RUN apk add --no-cache dumb-init
@ -66,13 +63,22 @@ WORKDIR /app
RUN addgroup -g 1001 -S nodejs && \ RUN addgroup -g 1001 -S nodejs && \
adduser -S bitip -u 1001 adduser -S bitip -u 1001
# Copy backend production files # Copy workspace configuration
COPY --from=backend-builder /app/backend/dist ./dist COPY package*.json ./
COPY --from=backend-builder /app/backend/package*.json ./
RUN npm ci --only=production && npm cache clean --force
# Copy built frontend # Copy backend workspace package files
COPY --from=frontend-builder /app/frontend/dist ./dist/frontend COPY src/backend/package*.json ./src/backend/
# Install production dependencies only (for backend)
RUN npm ci --only=production --workspace=bitip-backend && \
npm cache clean --force
# Copy built artifacts from builder
COPY --from=builder /app/dist ./dist
# Copy configuration files needed at runtime
COPY --from=builder /app/ReleaseNotes.json ./
COPY --from=builder /app/Overview.json ./
# Create directory for GeoIP databases # Create directory for GeoIP databases
RUN mkdir -p /usr/share/GeoIP && \ RUN mkdir -p /usr/share/GeoIP && \
@ -93,4 +99,4 @@ HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
# Start the application # Start the application
ENTRYPOINT ["dumb-init", "--"] ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "dist/index.js"] CMD ["node", "dist/backend/index.js"]