Debugging NodeJS within a Docker Container

Ritik Chourasiya
5 min readApr 20, 2023

--

Debugging NodeJS within a Docker Container
Debugging NodeJS within a Docker Container

As more and more software projects are being developed with NodeJS, Docker has become an increasingly popular tool for containerizing and managing these projects.

However, debugging NodeJS applications within a Docker container can be a bit tricky. In this article, we will go through the steps necessary to debug NodeJS within a Docker container.

💎What is Docker?

Docker is a containerization platform that allows developers to package their applications and their dependencies into a portable container that can be run anywhere, regardless of the underlying operating system or infrastructure. Containers are lightweight, fast, and easy to deploy, making Docker an ideal choice for modern software development.

If you haven’t already downloaded Docker — head over to the Docker website and download it today!

💎What is NodeJS?

NodeJS is an open-source, cross-platform, JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. NodeJS is commonly used for building scalable, high-performance web applications, and has a vast ecosystem of modules and libraries that make it a popular choice for modern software development.

If you haven’t already downloaded Docker — head over to the Official website of NodeJs and download it today!

💎Why Debug NodeJS within a Docker Container?

Debugging NodeJS within a Docker container has several benefits:

  1. Reproducibility: Debugging within a container ensures that the environment in which the code is running is identical across all instances of the container, ensuring consistent and reproducible results.
  2. Isolation: Debugging within a container ensures that any changes made to the debugging environment do not affect the host system or other containers running on the same system.
  3. Portability: Debugging within a container ensures that the debugging environment can be easily shared and replicated across different systems.

💎Debugging NodeJS within a Docker Container

Debugging NodeJS within a Docker container requires a few additional steps compared to debugging a NodeJS application running on the host system.

👉Step 1: Install the necessary dependencies

To debug a NodeJS application within a Docker container, you will need to install the following dependencies:

  • Docker
  • NodeJS
  • A NodeJS debugger (such as the built-in NodeJS debugger or the VS Code debugger)

👉Step 2: Create a Dockerfile

The Dockerfile is a script that contains instructions on how to build a Docker image. To create a Dockerfile for a NodeJS application, you will need to specify the base image, install NodeJS, copy the application files into the container, and expose the necessary ports. Here is an example Dockerfile:

# Specify the base image
FROM node:14

# Install the necessary dependencies
RUN apt-get update && \
apt-get install -y \
build-essential \
gcc \
g++ \
make

# Set the working directory
WORKDIR /app

# Copy the application files into the container
COPY package*.json ./
RUN npm install
COPY . .

# Expose the necessary ports
EXPOSE 3000

# Start the application
CMD ["npm", "start"]

👉Step 3: Build the Docker image

To build the Docker image, navigate to the directory that contains the Dockerfile and run the following command:

docker build -t my-node-app .

This will create a Docker image called my-node-app.

👉Step 4: Run the Docker container

To run the Docker container, run the following command

docker run -p 3000:3000 -it my-node-app

This will start the container and map port 3000 from the container to port 3000 on the host system. The -it flag specifies that the container should run in interactive mode.

👉Step 5: Debug the NodeJS application

To debug the NodeJS application running within the Docker container, you will need to attach a debugger to the running container. This can be done using the built-in NodeJS debugger or the VS Code debugger.

Using the built-in NodeJS debugger, you can add the --inspect flag to the npm start command in the Dockerfile, like so:

CMD ["node", "--inspect=0.0.0.0:9229", "index.js"]

This will start the NodeJS application with the debugger listening on port 9229. To attach a debugger to the container, open a new terminal window and run the following command:

node --inspect localhost:9229

This will attach a debugger to the NodeJS application running within the container, allowing you to debug the application as if it were running on the host system.

Alternatively, you can use the VS Code debugger to debug the NodeJS application running within the Docker container. To do this, you will need to create a .vscode/launch.json file in your project directory with the following configuration:

{
"version": "0.2.0",
"configurations": [
{
"name": "Docker: Attach to Node",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
]
}

This configuration tells the VS Code debugger to attach to a NodeJS application running on localhost:9229 within the Docker container. To start the debugger, click on the "Run" tab in VS Code and select the "Docker: Attach to Node" configuration.

💎Conclusion

Debugging NodeJS within a Docker container can seem daunting at first, but with the right tools and knowledge, it can be a straightforward process. By containerizing your NodeJS application, you can ensure reproducibility, isolation, and portability, making it easier to debug and deploy your application across different systems. With the built-in NodeJS debugger or the VS Code debugger, you can easily debug your NodeJS application running within a Docker container, allowing you to identify and fix bugs more quickly and efficiently.

Hope you find this article insightful 😉 💚

✨ Follow me on -

YouTube — https://www.youtube.com/@theritikchoure/

LinkedIn — https://www.linkedin.com/in/ritikchourasiya/

Twitter — https://twitter.com/theritikchoure

--

--

Ritik Chourasiya
Ritik Chourasiya

Written by Ritik Chourasiya

I’m a 22 year old, still undergraduate backend developer based in India, with 2 years of experience in the software development industry.