Reduce docker image size of nextJS
Tags: docker, nextjs
Published: 2022-10-02 18:23:53

---
title: Reduce docker image size of nextJS
published: true
description:
tags: docker, nextjs
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
---

I had the opportunity to work on a legacy project developed in React with nextJS framework. I am responsible for building the docker container and deploying it to the cluster. When I looked at a Dockerfile and make it, the size of the image was huge.


![Docker images huge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23ijpdkioudbmxu7kesy.png)


---

The legacy Dockerfile are

```Dockerfile
FROM node:16

WORKDIR /app
COPY . .
RUN yarn install --frozen-lockfile
CMD ["yarn","start"]
```

I got the first problem. In the Dockerfile.

## First Solution
I try to use multi-stage build for reduce docker images size.

```Dockerfile
FROM node:16 AS dependency

WORKDIR /app
COPY package.json .
COPY yarn.lock .
RUN yarn install --frozen-lockfile

FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=dependency /app/node_modules ./node_modules
COPY . .
RUN env
RUN yarn build

CMD [ "yarn","start" ]
```

But the size of the image is still not satisfactory.


![Docker images smaller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34bmjsxxcx8t3nagkzih.png)


## Second
I try to research the way to build and deploy nextjs application with docker images and I found the solution on next.js [Github](https://github.com/vercel/next.js/tree/canary/examples/with-docker)

1. Use Output Standalone
Edit next.config.js
```Javascript
module.exports = {
  // ... rest of the configuration.
  output: 'standalone',
}
```

2. Edit Dockerfile follow example.
```Dockerfile

FROM node:16-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
  else echo "Lockfile not found." && exit 1; \
  fi

FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN yarn build

FROM node:16-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"]

```

3. Ignore unnecessary files, I add Added .dockerignore to ignore that.

```text
node_modules
build
.dockerignore
Dockerfile
Dockerfile.*
```

## Result

The image size reduce from over 1GB to around 1xxMB

![Final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ly1dto5b113pbzv05ax.png)




## Use cache
In this project we use yarn as package dependency. and in the Dockerfile if we run 
```
yarn --frozen-lockfile
```

take a very long time so I changed a Dockerfile to use cache on package dependency.
```Dockerfile
FROM node:16-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY yarn.lock* package-lock.json* pnpm-lock.yaml* ./
# RUN \
#   if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
#   elif [ -f package-lock.json ]; then npm ci; \
#   elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
#   else echo "Lockfile not found." && exit 1; \
#   fi
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn yarn install --frozen-lockfile

...
```