Pav Sidhu
Pav Sidhu

Reputation: 6954

Debugging Node.js applications from inside Docker

When debugging a Node.js application, I expect the command node index.js --inspect=5858 to make my index.js file debuggable on the port 5858.

When I run the application outside of Docker, on my host computer, I'm able to debug the application in Chrome Dev Tools. I did the same inside of Docker and Chrome Dev Tools is unable to access the debugger on port 5858.

As you can see, port 5858 is exposed to the host computer for debugging:

version: '2'
services:
  server:
    build: .
    command: node index.js --inspect=5858
    volumes:
     - .:/app
    ports:
     - "8000:8000"
     - "5858:5858"
    environment:
     - NODE_ENV = "development"

I've specified localhost:5858 and 127.0.0.1:5858 network endpoints in Chrome Dev Tools, though still Dev Tools does not connect to it automatically as it suggests.

This seems to be an issue with my Docker setup though as you can see, I have exposed the port to the host computer. What could be the issue? Thanks.

Upvotes: 0

Views: 222

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

The issue is that when you are running it inside the container it is listening only to localhost by default. So you have two options

Use host network

Add network_mode: host to your service and remove the port mappings. Not an ideal thing to do

Make sure nodejs listens to every IP

Change

command: node index.js --inspect=5858

to

command: node --inspect=0.0.0.0:5858 index.js

Upvotes: 3

Related Questions