Forem

Cover image for Create Multi Stage Docker Image
Khaled Md Saifullah
Khaled Md Saifullah

Posted on

Create Multi Stage Docker Image

Docker is an open platform for developing, shipping and running applications. Modern application deployment requires the creation of effective and optimized Docker images. Docker images frequently grow excessively which affects resource utilization and performance. Docker's multi stage build feature offers a simplified method of reducing image size without sacrificing functionality in order to solve this issue. In this article, I'll teach you how to optimize images professionally and effectively by creating a multi stage Docker image for a Node.js application.

What is Multi Stage Docker Image?

A multi-stage Docker image is a Docker build approach that optimizes the build process by using numerous stages in a single Dockerfile. By keeping the construction and runtime environments separate this method enables you to produce Docker images that are lean and effective.

Building the Node.js Application

  • Step-1: Initialize a Node app
npm init -y
Enter fullscreen mode Exit fullscreen mode
  • Step-2: Install dependencies Here, I just need one dependency and that is express
npm i express
Enter fullscreen mode Exit fullscreen mode
  • Step-3: Create a file as app.js
touch app.js
Enter fullscreen mode Exit fullscreen mode
  • Step-4: Write the application code
import express from "express";

const app = express();

app.get("/", (req, res) => {
  return res.status(200).json({ message: "hello" });
});

app.listen(8080, () => console.log("server is listening"));
Enter fullscreen mode Exit fullscreen mode
  • Step-5: Add the type and required scripts in package.json file
"type": "module",
"scripts": {
    "start": "node app.js"
 },
Enter fullscreen mode Exit fullscreen mode

Prepare the Dockerfile

# stage 1 
FROM node:22 AS backend-builder

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . . 

# stage 2
FROM node:22-slim 

WORKDIR /app 

COPY --from=backend-builder /app .

EXPOSE 8080

CMD [ "npm","start" ]
Enter fullscreen mode Exit fullscreen mode

In the initial phase of the multi-phase build process the most recent Node.js image, node:22 is utilized as the foundation image. This step involves creating a working directory and copying the required files in order to get ready for the build.
A lighter and better optimized version of the base image node:22-slim is used in the second stage. This is the point at which the image size is drastically reduced by optimization. Ultimately, the application is configured by opening the necessary port and launching it.

Build the image

Then you need to build the image by using the following command

docker build -t mini-backend .
Enter fullscreen mode Exit fullscreen mode

Run the container

In this step we need to run a container based on the image.

docker run -d -p 8080:8080 --name mini-backend mini-backend
Enter fullscreen mode Exit fullscreen mode

Test the Application

Just open your browser and search for http://localhost:8080 you will see the following output in your browser.

{
  "message": "hello"
}
Enter fullscreen mode Exit fullscreen mode

In the other way you can check the output using the terminal. Write curl http://localhost:8080 in your system's terminal, you will get the same output.

Conclusion

Docker's multi stage builds which divide build and runtime stages help in producing lean optimized images. I have just explained how to effectively deploy a lightweight Node.js application in this post. Try it out and streamline your deployments.

Top comments (0)