Next.js Discord

Discord Forum

Optimizing Docker Build Time for Next.js Applications

Unanswered
Toyger posted this in #help-forum
Open in Discord
Avatar
ToygerOP
want to reduce the build time of my Next.js application my docker is installing dependencies every time how i can fix this below is my docker file

# Stage 1: Build
FROM node:20-alpine AS builder


# Install dependencies
# Install dependencies
RUN apk add --no-cache build-base cairo-dev pango-dev jpeg-dev giflib-dev librsvg-dev




WORKDIR /app


# Copy package.json and install dependencies
COPY package.json .npmrc ./
ENV NODE_OPTIONS="--max-old-space-size=8192"
RUN yarn install --frozen-lockfile --test --verbose


# Build the project
COPY . .
RUN yarn build


# Stage 2: Runtime
FROM node:20-alpine AS runner


# Install runtime dependencies required by the canvas library
# Install runtime dependencies required by the canvas library
RUN apk add --no-cache cairo pango jpeg giflib librsvg




WORKDIR /app
ENV NODE_ENV=test


# Copy necessary files from the build stage
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/webpack.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json


# Ensure shared libraries are in the correct path
# RUN ldconfig


EXPOSE 3000
CMD ["npm", "start"]

if its not relavant to this community please delete thanks in advance

0 Replies