0% found this document useful (0 votes)
6 views1 page

Node.js Multi-Stage Docker Setup

This document outlines a multi-stage Dockerfile for building and running a Node.js application. It consists of three stages: installing dependencies, building the application, and setting up the runtime environment with PM2. The final image exposes port 8000 and uses PM2 to start the application in cluster mode.

Uploaded by

aditya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Node.js Multi-Stage Docker Setup

This document outlines a multi-stage Dockerfile for building and running a Node.js application. It consists of three stages: installing dependencies, building the application, and setting up the runtime environment with PM2. The final image exposes port 8000 and uses PM2 to start the application in cluster mode.

Uploaded by

aditya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# First stage: Build the application

FROM node:22-alpine AS deps

ENV DEBIAN_FRONTEND=noninteractive

# Set the working directory


WORKDIR /app

# Install build dependencies


COPY [Link] [Link] ./

# Install dependencies
RUN yarn install --frozen-lockfile --non-interactive && rm -rf /root/.cache

# Second stage: Build the application


FROM node:22-alpine AS build

ENV DEBIAN_FRONTEND=noninteractive

# Set the working directory


WORKDIR /app

# Copy the necessary files from the build stage


COPY --from=deps /app/node_modules ./node_modules

# Copy the rest of the application code


COPY . .

# Install dependencies, build the application, and clean up


RUN yarn global add typescript && yarn run build && rm -rf /app/src /root/.cache

# Third stage: Run the application


FROM node:22-alpine AS runtime

# Set the working directory


WORKDIR /app

# Install PM2 globally and clean up


RUN yarn global add pm2@latest && rm -rf /root/.cache
RUN mkdir -p logs

# Copy only the necessary files from the build stage


COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/[Link] ./[Link]
COPY --from=build /app/[Link] ./[Link]

# Expose the port the app will run on


EXPOSE 8000

# Use PM2 to start the application in cluster mode


CMD ["yarn", "start"]

You might also like