DEV Community

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

Posted on

4

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay