Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 28, 2023 09:28 am GMT

Reduce Docker Image size for your Next.js App

Introduction

First thing first, I expect you to know what is Docker, but if you dont:

Docker is an open platform for developing, shipping, and running applications

You can spend time learning about it

NextJS, in other hand, is a flexible React framework that gives you building blocks to create fast web applications.

NextJS, in other hand, is a flexible React framework that gives you building blocks to create fast web applications.

Dockerize your App

Before we optimize anything, we have to dockerize the application first. Lets say our application name is my-space . Starting with:

Create the Dockerfile :

touch Dockerfile

Ignore unnecessary files in dockerignore:

node_modules.next.vscode.gitignoreREADME.md.dockerignore.git

Dockerize it:

Image description

This is the most basic example on how to dockerize your app, now lets build it with:

docker build -t my-space .

Now look at the size:

Image description

*Thats just crazy, 2.42gb!!
*

Unbelievable right, we cant publish this image, its just too heavy !

Reduce the image size

Use alpine

The Node.js Docker team maintains a node:alpine image tag and variants of it to match specific versions of the Alpine Linux distributions with those of the Node.js runtime. The original version size is about 1gb.

Now we will move to the alpine version:

Image description

Image description

Now the size shrank to 1.65gb, 800mb smaller. Thats a good start !

Multi-stages builds

Multi-stage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain.

We will create 2 stages in the Dockerfile . I will call it builder and runner

In this way we can get rid of unnecessary files in our image:

Image description

We will pick files from the builder and move it to the runner that we will eventually use:

Image description

*The size comes down to 1.36gb, about 300mb smaller, we are doing good !
*

Remove duplicate layers

You can see something is duplicating. Yes, we install the dependencies twice for each stage. Although this works and the project size is still the same. The image size is still big because of caching and layers.

So we can pick the node_modules from the build stage:

Image description

Image description

The size now is quite decent for a NextJS app, below 500mb

But we can still make it lighter !

Output File Tracing

During a build, Next.js will automatically trace each page and its dependencies to determine all of the files that are needed for deploying a production version of your application.

This feature helps reduce the size of deployments drastically. Previously, when deploying with Docker you would need to have all files from your packages dependencies installed to run next start. Starting with Next.js 12, you can leverage Output File Tracing in the .next/ directory to only include the necessary files.

In your next.config.js file, enable the standalone output

experimental: {    outputStandalone: true,  },

This will create a folder at .next/standalone which can then be deployed on its own without installing node_modules.

Image description

Image description

*The size is now 176mb! Small enough for most cases
*

Conclusion

This is just a simple example on how to optimize your docker image sizes, you can look deeper in Docker docs to find the most suitable treatment for your app!


Original Link: https://dev.to/leduc1901/reduce-docker-image-size-for-your-nextjs-app-5911

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To