DEV Community

Cover image for How to Containerize a Node.js Application
Khaled Md Saifullah
Khaled Md Saifullah

Posted on

How to Containerize a Node.js Application

Docker is a open source platform for developing, shipping and running applications. Here, We are going to containerize a simple Node.js application.

Project Setup

Image description

First, We will build a Node.js application. After that We will write some commands or instruction to a Dockerfile for building a Docker Image. Finally we are able to run a container form based on that image.

Building the Node.js Application

Step-01: Create a Node Application

Create a new directory/folder in our machine. Navigate to that directory. Then execute the command bellow to create a brand new Node application.

npm init -y
Enter fullscreen mode Exit fullscreen mode

Step-02: Make Some Changes in Our Package.json File

{
  "main": "server.js",
  "type": "module",
  "scripts": {
    "start": "node server.js",
    "dev": "node --watch server.js"
  }
}
Enter fullscreen mode Exit fullscreen mode

Step-03: Install Required Dependencies

In this case you need only one npm package that is express. Open your terminal in the root of your project. Then execute the command bellow

npm install express
Enter fullscreen mode Exit fullscreen mode

Step-04: Write Your Application

In this step you can write your code to build the application. You are writing code to create a very basic application. The code has written below

import express from "express"

const app = express()

const PORT = process.env.PORT || 8000
const hostname = "0.0.0.0"

app.get('/',(req,res)=>{
  res.send("<h1>Hello from the Docker Home Page</h1>")
})

app.get('/about',(req,res)=>{
  res.send("<h1>Hello from the Docker About Page</h1>")
})

app.listen(PORT,hostname,()=>console.log("Server is running...."))

Enter fullscreen mode Exit fullscreen mode

Build the Docker Image

Step-01: Create Dockerfile

Now, It's time to build your docker Image. Fist we need to create a new file as Dockerfile in your application root directory.

touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

Step-02: Write the Necessary Commands in Dockerfile

FROM node:22-alpine
WORKDIR /app
COPY package*.json /app
RUN npm install
COPY . /app
EXPOSE 8000
CMD [ "npm", "start" ]
Enter fullscreen mode Exit fullscreen mode

Here, every line of command represent an individual layer of the image. In the fist line of code I have declared the base image to run the node application. We can choose any node image from the docker hub.

The second line of command we created a new directory in the docker image as app.

The third line of command indicates that we copy package.json and package-lock.json file to the app directory.

In the fourth line of command install all required dependencies according to our package.json file.

Fifth line of command will copy our source code along with all directories and files to the app directory.

Sixth line of command used for port mapping. Here you need to define your application port or expose your application port.

Using the final command you need to start your node application on the container.

Step-02: Build the Docker Image

Now, open your terminal. Then execute the command to build the docker image.

docker build -t your-image-name .
Enter fullscreen mode Exit fullscreen mode

Step-03: Run the Container

Based on the image we can run a container.

docker run -d -p 8000:8000 --name your-container-name your-image-name 
Enter fullscreen mode Exit fullscreen mode

After run this command your have a running container that will run our node application. We can verify our container is running or not using docker ps command.

Test the Application

We can now test our application is running perfectly okay or not. We can do this in two ways, first one is using terminal and second way is using our browser.

First Way
Open your terminal in the root of your project. Run the command below

curl 127.0.0.1:8000
Enter fullscreen mode Exit fullscreen mode

If the above command return "Hello from the Docker Home Page", that means your container is running fine.

Second Way
We can check your application by using our browser. Go to your browser. In the search bar type http://127.0.0.1:8000. If we see the "Hello from the Docker Home Page" that means our application is running perfectly okay. We can also change the route and will see another output using http://127.0.0.1:8000/about.

Conclusion

That all for this simple guide to containerize a very very basic Node.js application. Hope, from this guide you learn something new.

Happy Coding...👍

Top comments (0)