![]() |
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. Table of Content
PrerequisitesWhat is Docker ImageWe 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
How to create a Docker ImageWe 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 DockerfileThe Dockerfile includes instructions for the Docker Image. Docker reads the Dockerfile and automatically creates image. So common Dockerfile commands are
In the next part, we will be seeing a example of actual Docker Image. Build Image from DockerfileNow open the terminal and go the same directory where the Dockerfile is situated and execute the following command docker build -t <image_name> .
Dockerize a Node.js appllicationNode.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 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 Use docker build command to build the image. In the following steps we will optimize this image. Efficient Dockerfile Strategies for Node.jsImage optimization means reducing size and making it more stable in production environment. 1. Choosing the Right Base ImageWe 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.
Solution to these problems are quite simple.
FROM node:22.3.0-bullseye-slim The size of the current image is 348 MB which is already less than the initial image. 2. Leveraging .dockerignore to Exclude Unnecessary FilesThis file contains all the files and images you want to exclude from image. This increases the performance of docker. node_modules 3. Using npm ci Instead of npm installThe 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 4. Use multi-stage Builds for Smaller ImagesIdea 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 5. Minimizing Layers to Reduce Image SizeEach 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 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. ConclusionIn 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. Docker Image Optimization for Node.js – FAQsHow can I use specific version of Node.js?
How much reduction of size can be achieved by using multistage docker build
How can I achieve highest size reduction
|
Reffered: https://www.geeksforgeeks.org
DevOps |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |