Optimized Dockerfile to produce a smaller and faster build
This commit is contained in:
@@ -1,53 +1,60 @@
|
|||||||
# Stage 1: Build frontend
|
# Stage 1: Build frontend
|
||||||
FROM --platform=$BUILDPLATFORM node:18 AS frontend-builder
|
FROM --platform=$BUILDPLATFORM node:18-alpine AS frontend-builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install
|
RUN npm ci --only=production
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# Stage 2: Build backend
|
# Stage 2: Build backend
|
||||||
FROM --platform=$BUILDPLATFORM node:18 AS backend-builder
|
FROM --platform=$BUILDPLATFORM node:18-alpine AS backend-builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install
|
RUN npm ci --only=production
|
||||||
COPY src/backend/ ./src/backend/
|
COPY src/backend/ ./src/backend/
|
||||||
|
|
||||||
# Stage 3: Final production image
|
# Stage 3: Build bcrypt for the target platform
|
||||||
FROM mongo:4.4
|
FROM --platform=$TARGETPLATFORM node:18-alpine AS bcrypt-builder
|
||||||
# Install Node.js
|
WORKDIR /app
|
||||||
RUN apt-get update && apt-get install -y \
|
COPY package*.json ./
|
||||||
curl \
|
RUN apk add --no-cache python3 make g++ \
|
||||||
nginx \
|
&& npm ci --only=production bcrypt \
|
||||||
python3 \
|
&& rm -rf /root/.npm
|
||||||
build-essential \
|
|
||||||
&& curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
|
|
||||||
&& apt-get install -y nodejs \
|
|
||||||
&& apt-get clean \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
# Configure nginx
|
# Stage 4: Final production image
|
||||||
|
FROM mongo:4.4-focal
|
||||||
|
# Install Node.js and nginx, cleanup in the same layer
|
||||||
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
nginx \
|
||||||
|
curl \
|
||||||
|
&& curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
|
||||||
|
&& apt-get install -y --no-install-recommends nodejs \
|
||||||
|
&& apt-get clean \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* \
|
||||||
|
&& rm -rf /var/cache/apt/*
|
||||||
|
|
||||||
|
# Configure nginx and copy frontend
|
||||||
COPY docker/nginx.conf /etc/nginx/nginx.conf
|
COPY docker/nginx.conf /etc/nginx/nginx.conf
|
||||||
COPY --from=frontend-builder /app/dist /usr/share/nginx/html
|
COPY --from=frontend-builder /app/dist /usr/share/nginx/html
|
||||||
|
|
||||||
# Setup backend
|
# Setup backend with pre-built bcrypt
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install --omit=dev
|
RUN npm ci --only=production --ignore-scripts
|
||||||
|
COPY --from=bcrypt-builder /app/node_modules/bcrypt /app/node_modules/bcrypt
|
||||||
COPY --from=backend-builder /app/src/backend ./src/backend
|
COPY --from=backend-builder /app/src/backend ./src/backend
|
||||||
|
|
||||||
# Create directories for MongoDB and nginx
|
# Create necessary directories and set permissions
|
||||||
RUN mkdir -p /data/db && \
|
RUN mkdir -p /data/db /var/log/{nginx,mongodb} /var/lib/nginx \
|
||||||
mkdir -p /var/log/nginx && \
|
&& chown -R mongodb:mongodb /data/db /var/log/mongodb \
|
||||||
mkdir -p /var/lib/nginx && \
|
&& chown -R www-data:www-data /var/log/nginx /var/lib/nginx \
|
||||||
mkdir -p /var/log/mongodb && \
|
&& rm -rf /root/.npm /tmp/*
|
||||||
chown -R mongodb:mongodb /data/db /var/log/mongodb && \
|
|
||||||
chown -R www-data:www-data /var/log/nginx /var/lib/nginx
|
|
||||||
|
|
||||||
# Set environment variables
|
# Set environment variables
|
||||||
ENV MONGO_URL=mongodb://localhost:27017/termix \
|
ENV MONGO_URL=mongodb://localhost:27017/termix \
|
||||||
MONGODB_DATA_DIR=/data/db \
|
MONGODB_DATA_DIR=/data/db \
|
||||||
MONGODB_LOG_DIR=/var/log/mongodb
|
MONGODB_LOG_DIR=/var/log/mongodb \
|
||||||
|
NODE_ENV=production
|
||||||
|
|
||||||
# Create volume for MongoDB data
|
# Create volume for MongoDB data
|
||||||
VOLUME ["/data/db"]
|
VOLUME ["/data/db"]
|
||||||
@@ -55,7 +62,7 @@ VOLUME ["/data/db"]
|
|||||||
# Expose ports
|
# Expose ports
|
||||||
EXPOSE 8080 8081 8082 27017
|
EXPOSE 8080 8081 8082 27017
|
||||||
|
|
||||||
# Use a entrypoint script to run all services
|
# Copy and set entrypoint
|
||||||
COPY docker/entrypoint.sh /entrypoint.sh
|
COPY docker/entrypoint.sh /entrypoint.sh
|
||||||
RUN chmod +x /entrypoint.sh
|
RUN chmod +x /entrypoint.sh
|
||||||
CMD ["/entrypoint.sh"]
|
CMD ["/entrypoint.sh"]
|
||||||
Reference in New Issue
Block a user