DEV Community

Cover image for Docker Applied Skills: Containerize a Node.js application
John Ogbonna
John Ogbonna

Posted on

1

Docker Applied Skills: Containerize a Node.js application

Docker Applied Skills: Containerize a Node.js Application

Introduction

Docker is a powerful tool for containerizing applications, allowing developers to package their code and dependencies into lightweight, portable containers. In this guide, we’ll walk through the process of containerizing a Node.js application using Docker. We will be following the steps outlined here

By the end of this tutorial, you will:

  • Set up a simple Node.js application.
  • Write a Dockerfile to containerize the application.
  • Build and run a Docker container.
  • Manage the container lifecycle using Docker commands.

Why containerize apps?

Containerizing apps offers several advantages:

  • Consistency: Ensures apps run the same across all environments (dev, test, prod).
  • Portability: Easily deployable across different platforms and cloud services.
  • Isolation: Prevents conflicts between app dependencies.
  • Scalability: Easily scale applications based on demand.
  • Faster Deployment: Containers are lightweight and start quickly.
  • Version Control: Manage and roll back app versions easily.
  • Resource Efficiency: Containers use fewer resources than VMs.
  • Security: Isolates apps for better security and risk management. Containerization streamlines development, deployment, and scalability, improving consistency, efficiency, and security.

Prerequisites

Before you begin, ensure you have the following installed on your system:

Let’s get started!

Getting the Sample Application

In your terminal/powershell, navigate to a folder where you'd like to store this sample application. To get the sample app, enter:

git clone https://github.com/docker/docker-nodejs-sample && cd docker-nodejs-sample
Enter fullscreen mode Exit fullscreen mode

Then enter:

ls
Enter fullscreen mode Exit fullscreen mode

To confirm that the files were brought in from git
brought in from git

Initialize Docker assets

To create the Docker assets necessary to containerize the application, we can use Docker's built in Docker init feature to streamline the process. Conversely, we can also create these assets manually. In this example, we will use Docker init
In the terminal enter:

docker init
Enter fullscreen mode Exit fullscreen mode

Answer these prompts accordingly:

What application platform does your project use? Node
What version of Node do you want to use? (ensure latest version ex. 21.5.0)
Which package manager do you want to use? npm
What command do you want to use to start the app: node src/index.js
What port does your server listen on? 3000
Enter fullscreen mode Exit fullscreen mode

App setup

Enter ls in the terminal. You should see these files:
You should see these files

Inside the docker-nodejs-sample directory, run the following command in a terminal to build the app:

docker compose up --build
Enter fullscreen mode Exit fullscreen mode
  • If you get an error similar to this, refer to the next step:
    error similar to this

  • To bypass file errors, modify the Dockerfile. Locate the CMD line and ensure that is says

CMD ["node", "src/index.js"]
Enter fullscreen mode Exit fullscreen mode
  • Like this:
    cmd line

  • Run

docker compose up --build
Enter fullscreen mode Exit fullscreen mode

again and it should work. You should see a message in the terminal like this:
like this

Open your browser and navigate to http://localhost:3000/. You should see something like:
should see something like

Summary:

This guide walks through the process of containerizing a Node.js application using Docker. The steps include setting up a Node.js app, writing a Dockerfile to containerize it, building and running the Docker container, and managing the container lifecycle with Docker commands.

Key steps include:

  • Containerization benefits: Docker ensures consistency, portability, isolation, scalability, faster deployment, and better security.

  • Getting the Sample App: Clone the sample node.js app from GitHub.
    Initialize Docker Assets: Use Docker’s docker init to streamline the process of creating necessary Docker files for the project.

  • Build and Run the App: Use docker compose up --build to build the containerized app and fix any errors that might arise during the process.

  • The node.js application will be successfully containerized and running locally using Docker.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

If you appreciated this article, please consider leaving a ❤️ or sharing a kind word!

Absolutely