DEV Community

Cover image for Interactive debugging for NodeJS apps in Docker
Abhinav Sachdeva
Abhinav Sachdeva

Posted on

Interactive debugging for NodeJS apps in Docker

Debugging can get hectic and with a bunch of console.logs you could still be missing that one variable that needs to be printed.
Interactive debugging is nothing new for NodeJS developers, the Node devtools has been around for quite a while and here's how you can work with it while running your app inside docker.

Inside the docker compose:

  • Expose the debugger port on your app
  • Start the app with the inspect flag


services:
  user-service:
    image: node:18-alpine
    ports:
      - '3000:3000'
      - '9228:9228' #Debugger port
    working_dir: /app
    volumes:
        - ./src:/app/src
        - ./node_modules:/app/node_modules
    environment:
      VAR1: value1
      VAR2: value2
    command: node --inspect=0.0.0.0:9228 ./src/index.js # Inspect mode


Enter fullscreen mode Exit fullscreen mode

Head over to the Chrome browser and open up devtools, you should have the NodeJS devtools icon available.

Node Inspector Client

If not, open chrome://inspect/ in Chrome and click on "Open dedicated DevTools for Node". Also, make sure that the URL and port you're connecting to are configured as discoverable.

Inspector configuration

BONUS TIP
If you'd like to have the VS Code debugging environment, add this file at the root of your workspace. Once added, goto the debugger panel and click on the play icon against the configuration name.



// .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Docker: Node.js",
            "type": "node",
            "request": "attach",
            "port": 9228,
            "localRoot": "${workspaceFolder}/src",
            "remoteRoot": "/app/src",
            "restart": true
        }
    ]
}


Enter fullscreen mode Exit fullscreen mode

Debug in VS Code

Top comments (0)