LIVE Instructor-Led Courses
How to Containerize and Deploy a Node.js Application with Docker - A Step-by-Step Guide

11 April 2023

How to Deploy a Node.js Application with Docker

This article is brought to you by JBI Training, the UK's leading technology training provider.   Learn more about JBI's JavaScript (Advanced) training courses including Microservices Architecture, ECMAScript 6 Introduction, PNodeJS (advanced)Data Science and AI/ML (Python)WebAssembly & DevOps Introduction


Deploying a Node.js application can be challenging, especially when it comes to ensuring consistency across different environments. Docker provides an easy way to containerize and deploy Node.js applications, allowing you to package your application and its dependencies into a single image that can be run anywhere. In this guide, we will walk you through the steps to deploy a Node.js application with Docker.


  • Basic understanding of Node.js and Docker
  • Node.js installed on your local machine
  • Docker installed on your local machine

Step 1: Create a Node.js Application

First, let's create a simple Node.js application that we can deploy with Docker. Create a new directory for your project and initialize a Node.js project with the following command:

mkdir myapp cd myapp npm init

Follow the prompts to initialize your project, and then create a new file called app.js with the following code:


const http = require('http'); const hostname = ''; const port = process.env.PORT || 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World!\n'); }); server.listen(port, hostname, () => { console.log(`Server running at https://${hostname}:${port}/`); });

This is a simple HTTP server that listens on the specified port and responds with "Hello, World!" for any incoming requests.

Step 2: Create a Dockerfile

Next, we will create a Dockerfile that will define the instructions for building our Docker image. Create a new file called Dockerfile in your project directory with the following contents:


# Use the official Node.js 14 image as a parent image FROM node:14 # Set the working directory to /app WORKDIR /app # Copy the package.json and package-lock.json files to the working directory COPY package*.json ./ # Install the dependencies RUN npm install # Copy the rest of the application code to the working directory COPY . . # Expose the port that the application will listen on EXPOSE 3000 # Start the application CMD [ "npm", "start" ]

This Dockerfile starts with the official Node.js 14 image, sets the working directory to /app, copies the package.json and package-lock.json files to the working directory, installs the dependencies with npm install, copies the rest of the application code to the working directory, exposes port 3000 (the port that our Node.js application listens on), and starts the application with npm start.

Step 3: Build the Docker Image

Now that we have our Dockerfile defined, we can build our Docker image with the following command:

docker build -t myapp .

This command builds the Docker image with the tag myapp using the current directory (.) as the build context. This may take a few minutes to complete, depending on your system and the size of your application.

Step 4: Run the Docker Container

Once the Docker image has been built, we can run the container with the following command:

docker run -p 3000:3000 myapp

This command runs the Docker container with the myapp image and maps port 3000 on the host to port 3000 in the container. You should see output similar to the following:

Server running at

This indicates that your Node.js application is now running inside a Docker container and listening on port 3000.

Step 5: Test the Application

To test the application, open a web browser and navigate to https://localhost:3000. You should see the message "Hello, World!" displayed in your browser. This confirms that your Node.js application is working correctly inside the Docker container.

Use Cases:

Containerizing Node.js applications with Docker has become a popular method for deployment. By containerizing your application, you can ensure that it runs consistently across different environments, and it's easier to manage and scale your application. This guide can be used by developers who want to containerize their Node.js application and deploy it to different environments, including production, staging, and development environments.


In this guide, we walked you through the steps to containerize and deploy a Node.js application with Docker. We covered how to create a Dockerfile, build the Docker image, run the container, and test the application. By following these steps, you can easily deploy your Node.js application using Docker, ensuring consistency and reliability across different environments.

Here are some official Docker documentation links related to containerizing and deploying Node.js applications:

  1. Dockerizing a Node.js web app
  2. Node.js and Docker
  3. Dockerize Your Node.js Application

These links provide in-depth documentation and tutorials for containerizing and deploying Node.js applications with Docker, including best practices, troubleshooting, and advanced topics.

We offer all forms of technical training please find course suggestions below. 

About the author: Daniel West
Tech Blogger & Researcher for JBI Training

+44 (0)20 8446 7555

[email protected]



Copyright © 2023 JBI Training. All Rights Reserved.
JB International Training Ltd  -  Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS

Privacy Policy | Terms & Conditions | Contact Us


Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course