How To Debug Node JS Inside Docker? | by Arjav Dave | May, 2022

Debug your manner into docker containers

debug node.js inside docker

For any developer, the debugger is one of the best buddy. One can simply discover bugs in software program with a debugger.

One can add a breakpoint to pause execution. Secondly, one may add logic to a breakpoint to halt the execution. For example, think about a for loop having 1,000 iterations. The execution ought to cease when the iteration rely reaches above 100. To take action, put a breakpoint on the for loop. Subsequent, add the logic to halt the execution when the iteration goes above 100.

Moreover halting a program, debuggers present reminiscence allocations. For instance, halting the execution will present reminiscence consumed at any given level.

Debugging is normally executed on a localhost. Doing it remotely is named distant debugging :). That’s, if you happen to debug software program operating on a distant host, its referred to as distant debugging. It’s useful for a number of causes.

For one, one can debug software program domestically. Think about a state of affairs the place software program is on the cloud. It is perhaps deployed both for dev, UAT, or manufacturing. Now a difficulty occurs on the cloud however not on the localhost. On this case, it will be very useful to connect with the cloud and connect the debugger to the method. One can execute the software program line by line to guage the problem and repair it.

Secondly, distant debugging can be helpful when the software program is operating inside a container. Let’s say a challenge is operating inside Docker. One gained’t be immediately capable of run the challenge and connect with it through the debugger. As an alternative, the docker container ought to expose its container port. Secondly, the distant debugger wants configuration to attach the challenge contained in the docker container.

Docker helps create moveable containers which can be quick and straightforward to deploy on numerous machines. These containers will be run domestically in your Home windows, Mac & Linux. Additionally, main cloud methods like AWS or Azure do assist them out of the field. If you wish to study extra Docker fundamentals and wish a cheat sheet for Docker CLI, here is an introductory article about it.

On this article, we are going to arrange a NodeJS challenge to run inside a docker container. We will even arrange a distant debugging for the challenge.

In the event you love this text up to now, please follow me and do try different such superior articles on my profile.

Earlier than we transfer additional, the system ought to have docker desktop and VS Code put in. Aside from that, no different necessities are there.

For the hasty ones, I’ve made the supply code obtainable as a repository. You’ll be able to test it out here.

We’re going to create a quite simple specific Node JS challenge. It would merely return a static JSON string on opening a particular URL. For this, we are going to create a file named server.js, which is the entry level to our challenge.

Create a server.js file with the next contents:

const server = require("specific")();
server.hear(3000, async () => );
server.get("/node-app", async (_, response) =>
response.json( "node": "app" );

The server.js file states that show “node”: “app” on opening http://localhost:3000/node-app URL within the browser.

Secondly, we are going to want a package deal.json file to configure the challenge and add dependencies. For that, create a package deal.json file with the next content material:

"identify": "node-app",
"specific": "^4.17.1"

Run the npm set up command to put in the dependencies domestically. This may create a node_modules within the challenge listing.

Although we can be operating the challenge inside a container, the dependencies have to be put in. It’s wanted since we can be mapping our present challenge listing to a container challenge listing. It’s defined under how to take action:

A Dockerfile is required to run the challenge as a docker container. Create a Dockerfile with the next contents:

Right here, the challenge is about as much as run as a easy node server with out permitting any breakpoints. The container can be operating the challenge out of a node listing contained in the container. nodemon is put in globally within the container. It’s wanted for watching any file change within the listing. It’s defined intimately under.

The RUN npm set up command is required solely when deploying to manufacturing. We are going to map the /node directosry of our container to the present challenge listing on localhost utilizing Docker Compose (subsequent part). However when the app is deployed on the container, it wants to put in the dependencies by itself.

The Docker ignore characteristic could be very a lot much like git ignore. .gitignore doesn’t observe the recordsdata or folders talked about in it. Equally, we don’t need to copy pointless recordsdata within the container, which takes up house.

In our case, we don’t need to copy the node_modules folder to the container. To take action, create a .dockerignore file within the challenge listing with the next contents:


Docker Compose is a very useful approach to construct and run docker containers with a single command. Additionally it is useful for operating a number of containers on the similar time. It is likely one of the causes we use docker compose as a substitute of plain docker. To know extra about docker compose and learn how to run a number of containers, please go to the article Run Multiple Containers With Docker Compose.

Now, let’s create a docker-compose.yml file so as to add some extra configurations. Add the under contents to docker-compose.yml file as soon as created:

The docker-compose.yml file is defined point-wise under.

  1. Level to our present listing for constructing the challenge.
  2. Run the challenge utilizing nodemon, since if there are any modifications within the native listing, we need to restart the challenge within the docker with the modifications. Nodemon is a utility that can monitor for any modifications in your supply and mechanically restart your server.
  3. Bind our present listing to the /node listing utilizing volumes.
  4. Along with exposing and binding the 3000 port for the server, expose the 9229 for attaching the debugger.

Use the above docker-compose.yml file just for debugging.

The above docker-compose.yml exposes the debug port. As well as, it additionally screens for any file modifications contained in the container (which aren’t going to occur). Lastly, it maps the volumes of the container to the challenge listing.

For manufacturing, create a brand new file docker-compose-prod.yml with the next contents:

model: '3.4'
construct: .
command: node /node/server.js 3000
- "3000:3000"

It merely runs the challenge and exposes the 3000 port. We’re utilizing a number of docker compose recordsdata to handle separate environments. Verify the Operating a Undertaking part under to grasp learn how to run a challenge primarily based on completely different docker compose recordsdata.

Earlier than we will run the challenge, we nonetheless have to configure the debugger to connect with the container.

First, test when you have launch.json file created in your challenge. launch.json defines various kinds of configurations we will run for debugging. If it isn’t created, go to the RUN AND DEBUG tab on the left in your VS Code, as seen within the picture under:


Click on on the textual content that claims create a launch.json file. Earlier than you’ll be able to proceed, it should ask the kind of software to proceed. Choose Node.js. It would create a brand new launch.json file in your challenge with a default Node.js configuration added.

Since we aren’t going to run the node software domestically, go forward and delete that configuration. As an alternative, exchange the launch.json file with the next content material:

The configuration added is fairly self-explanatory. Mainly, we’re asking the debugger to connect with a distant host with port quantity 9229. We’re additionally requesting the debugger to restart each time it will get disconnected to the host. By default, the debugger tries to attach on http://localhost:9229/. However challenge is hosted contained in the /node listing in docker. To map /node, the remoteRoot attribute is used.

That’s about it! Now, if you happen to run docker compose up, your challenge will begin operating. For the primary run, it should obtain some layers of the node slim SDK after which set up nodemon contained in the docker container. However, subsequent runs can be a lot sooner. Operating docker compose up will present the next output in your terminal:

docker compose up

With a purpose to connect the debugger, run the Docker: Connect to Node activity from the RUN AND DEBUG tab. The debugger will now connect to the /node listing of your docker container. Subsequent, put a breakpoint on line 4 of your server.js file, i.e., response.json( “tremendous”: “app1” );. Lastly, open your browser and hit http://localhost:3000. The breakpoint can be hit, and the execution will halt.

For manufacturing, we have to use the 1 file. To take action, we have to point out the file identify within the docker command. Execute the next command to run the challenge as if in a manufacturing setting:

docker compose -f docker-compose-prod.yml up

With the above command, a debugger can’t be hooked up to the container since we aren’t exposing any debugging level.

Right here is the link to the ultimate supply code of the challenge we’ve created.

Debugging is likely one of the greatest issues for improvement. It’s the cherry on prime once we are capable of debug remotely. Distant debugging allows us to connect with code operating not solely on the cloud but in addition to a docker container operating domestically.

I hope you could have loved this text. Be at liberty to take a look at a few of my different articles:

More Posts