Docker Image Optimization for Node.js
Last Updated :
12 Jul, 2024
Docker is the most widely containerization tool that developers to deploy applications to production with little to no downtime. In this article, we are going to learn how Docker images can be optimized for Node.js applications for better performance, stability, and security.
Prerequisites
- Docker installed
- Basic knowledge of Node.js
What is Docker Image
We know a container is an isolated environment, but from where container get files and configuration? A container image is a standardized package that includes all of the files, binaries, libraries, and configurations to run a container.
So, a Docker Image is basically the immutable blueprint that includes all the instructions needed to create the Docker Container.
Why Docker Image Optimization Matters
- Optimized images are smaller in size take fewer resources to build and reduce the time required for the build
- Optimized images require less CPU resources, disk I/O, and memory, so they perform better.
- Optimized images are easier to maintain as they are less complex and focus more on components.
- Could services charge for CPU and memory usage, so optimized images reduce the cost of running the service?
How to create a Docker Image
We can get many Docker images from the Docker hub but most of the time we need to make our own custom images. There are two steps for creating a Docker Image-
Steps To Write a Dockerfile
The Dockerfile includes instructions for the Docker Image. Docker reads the Dockerfile and automatically creates image. So common Dockerfile commands are
- FROM: A valid Dockerfile always starts with it(with only exception of ARG). This command creates a new build stage of the image.
- COPY: Copies files from previous stages or host OS.
- WORKDIR: Specifies working directory.
- EXPOSE: Exposed port of container to external world.
- RUN: Used to execute commands within a new layer while building the Docker image
- CMD: Defines the default command to be executed when a container is launched from the image which can be overridden by user.
- ENTRYPOINT: Same as CMD but can not be overridden by user.
In the next part, we will be seeing a example of actual Docker Image.
Build Image from Dockerfile
Now open the terminal and go the same directory where the Dockerfile is situated and execute the following command
docker build -t <image_name> .
- "-t" specifies the tag for the image.
- The "." at the end specifies same directory which can be changed depending on where the Dockerfile is locacted.
Dockerize a Node.js appllication
Node.js is a JavaScript runtime that is used to write server-side JavaScript applications.
Lets write a Dcokerfile for the node.js apllication
FROM ubuntu:latest
USER root
WORKDIR /app
RUN apt -y update && apt install -y curl && curl -sL https://deb.nodesource.com/setup_22.x | bash -
RUN apt install -y nodejs
COPY . .
RUN npm install
ENTRYPOINT ["npm", "run", "start"]
We are using ubuntu as base image and installing nodejs and coping files from host device to the container.
Use the following command to get the size of the image
docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
gfg-node-image-optimation latest e15df68a065d 2 days ago 471MB
Use docker build command to build the image. In the following steps we will optimize this image.
Efficient Dockerfile Strategies for Node.js
Image optimization means reducing size and making it more stable in production environment.
1. Choosing the Right Base Image
We are using a Node.js to create a http server. But the base Ubuntu image does not comes with Node.js pre-installed. So, we have to first install Node.js and npm and manage the version ourselves. This creates extra hassle and takes more space. Better approach would be directly using the Node.js official image as we do not have to do anything our self. Now, lets modify the Dockerfile. Always choose the official or verified from docker hub.
Most commonly used tag for node is nodejs image is latest which is alias of current-bullseye. But there are few problem with using it.
- It can give inconsistent result as it always searches for the latest version of the image.
- It uses a full fledged Debion linuz, so size is generally larger which is not intended.
Solution to these problems are quite simple.
- Always specify the version of image
- Use lightweight official images like alpine, bookworm-slim, bullseye-slim.
FROM node:22.3.0-bullseye-slim
WORKDIR /app
COPY . .
RUN npm install
ENTRYPOINT ["npm", "run", "start"]
The size of the current image is 348 MB which is already less than the initial image.
2. Leveraging .dockerignore to Exclude Unnecessary Files
This file contains all the files and images you want to exclude from image. This increases the performance of docker.
node_modules
.env
3. Using npm ci Instead of npm install
The npm install command installs all the dependencies including devDependencies which are not needed for production environment. So, we can use npm ci instead. It strictly works on the versions locked in package-lock.json, npm ci ensures that builds are reproducible across different environments. This predictability is crucial for maintaining consistency in production deployments.
FROM node:22.3.0-bullseye-slim
WORKDIR /app
COPY . .
RUN npm ci --only=production
ENTRYPOINT ["npm", "run", "start"]
4. Use multi-stage Builds for Smaller Images
Idea is to use multiple stages to build the image and copying only the necessary images from the previous stage to the new stage. This significantly reduces the size of the image and avoids leaking sensitive information.
FROM node:22.3.0-bullseye-slim AS builder
WORKDIR /app
COPY . .
RUN npm ci --only=production
RUN npm npm i -g @vercel/ncc
RUN ncc build index.js -o dist
FROM node:22.3.0-bullseye-slim
WORKDIR /app
COPY --from=builder /app/dist/index.js .
ENTRYPOINT ["node", "index.js"]
5. Minimizing Layers to Reduce Image Size
Each RUN, COPY, and ADD command creates a new layer. Combine commands to reduce layers. This can be done using running everything in one command. If you're using multistage docker build, then no need to do it.
FROM node:22.3.0-bullseye-slim AS builder
WORKDIR /app
COPY . .
RUN npm ci --only=production && npm npm i -g @vercel/ncc && ncc build index.js -o dist
ENTRYPOINT ["node", "index.js"]
Size comparison of different images
Here is the size comparison for the the images. The oldest the one is the oldest image was created using the first Dockerfile code we have written and the latest one uses multi-staged docker builds.
Conclusion
In this article, we have learned about docker images, procedure of creating docker images from Dockerfile and five techniques for optimizing the docker image for Node.j application.
Similar Reads
Next.js Image Optimization
Next.js provides built-in support for image optimization to improve the performance of your web applications. By using the next/image component, you can automatically optimize images on-demand as the users request them, ensuring fast load times and better user experiences.For image optimization, Nex
5 min read
How To Optimize Docker Image ?
Docker images are small executable packages that can be used to run a program along with its libraries, dependencies, code, and runtime. Docker images form the basis of Docker containers, which allow software to be deployed continuously across several environments. We will be talking more about Dock
10 min read
Next.js Docker Images
Using Next.js Docker images allows your app to deploy to multiple environments, and is more portable, isolated and scalable in dev and prod. Dockerâs containerization makes app management super easy, you can move from one stage to another with performance.Before we get started, letâs cover the basic
14 min read
Next JS Image Optimization: Best Practices for Faster Loading
Large and unoptimized images can impact a website's performance on loading time. Optimizing images is necessary to improve the performance of the website. Next.js provides built-in support for image optimization to automate the process, providing a balance between image quality and loading speed. Pr
4 min read
Log in to Docker for Pulling and Pushing Images
Docker is a platform that provides a set of PaaS ( Platform as a Service ) products that help developers containerize applications, to run them consistently in all supported environments. Docker utilizes virtualization at the OS-level to deliver software in containers. At its core Docker utilizes Do
4 min read
What is Docker Image Layer?
Docker has led a revolution in application development and operation by providing a powerful containerization platform. It makes isolated environments where applications can run the same way without considering the infrastructure difference. The core principle lying at the center of Docker's contain
9 min read
What is Docker Image?
Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the
10 min read
How to Optimize Image for better SEO?
The performance of your website in search results, the user experience, and the likelihood that your photographs will appear in image search results are all improved by image optimization, which is a crucial component of search engine optimization (SEO). Keep in mind that image optimization is about
9 min read
How To Rebuild the Docker Image ?
Docker has forever changed the way developers build, package, and deploy applications. It permits them to run applications in isolated environments that are mostly named containers, at the core of Docker is an image: a lightweight, stand-alone, and executable package that includes everything needed
6 min read
Docker - Publishing Images to Docker Hub
Docker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it pu
8 min read