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
# Stage 1: Build frontend
FROM node:24.9-alpine3.21 AS frontend-builder
# Stage 1: Build application
FROM node:24.9-alpine3.21 AS builder
WORKDIR /app/frontend
WORKDIR /app
# Copy frontend package files
COPY src/frontend/package*.json ./
RUN npm ci --only=production
# Copy workspace configuration
COPY package*.json ./
# Copy frontend source
COPY src/frontend/ ./
# Copy workspace package files
COPY src/backend/package*.json ./src/backend/
COPY src/frontend/package*.json ./src/frontend/
# Build frontend
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 ./
# Install all dependencies (including devDependencies for build)
RUN npm ci
# Copy backend source
COPY src/backend/ ./
# Copy source code
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
# Stage 3: Production image
# Stage 2: Production image
FROM node:24.9-alpine3.21
# 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.revision="${GIT_REVISION}"
# Set version environment variable (accessible at runtime)
# Set version environment variables (accessible at runtime)
ENV APP_VERSION=${APP_VERSION} \
CREATED_AT=${CREATED_AT} \
GIT_REVISION=${GIT_REVISION}
GIT_REVISION=${GIT_REVISION} \
NODE_ENV=production
# Install dumb-init for proper signal handling
RUN apk add --no-cache dumb-init
@ -66,13 +63,22 @@ WORKDIR /app
RUN addgroup -g 1001 -S nodejs && \
adduser -S bitip -u 1001
# Copy backend production files
COPY --from=backend-builder /app/backend/dist ./dist
COPY --from=backend-builder /app/backend/package*.json ./
RUN npm ci --only=production && npm cache clean --force
# Copy workspace configuration
COPY package*.json ./
# Copy built frontend
COPY --from=frontend-builder /app/frontend/dist ./dist/frontend
# Copy backend workspace package files
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
RUN mkdir -p /usr/share/GeoIP && \
@ -93,4 +99,4 @@ HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
# Start the application
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "dist/index.js"]
CMD ["node", "dist/backend/index.js"]