Search code examples
node.jsvisual-studio-codedocker-composedockerfilenodemon

Unable to debug a nodejs app inside of docker


I am trying my hands on debugging a Nodejs app from inside of Docker using Nodemon.

I can debug my app with Nodemon without Docker using Visual Studio Code's Auto Attach feature.

But when I build my docker image and start the container via npm run dev:docker:debug I get following log but debugger is not attached. It might be something with the volume but I am not able to figure it out...

Successfully built 857d9da57565
Successfully tagged app:dev
Creating docker_app_1 ... done
Attaching to docker_app_1
app_1  |
app_1  | > app@1.0.0 dev:debug /usr/src/app
app_1  | > nodemon --config nodemon.json --env=development
app_1  |
app_1  | [nodemon] 2.0.2
app_1  | [nodemon] to restart at any time, enter `rs`
app_1  | [nodemon] watching dir(s): src/**/*
app_1  | [nodemon] watching extensions: ts
app_1  | [nodemon] starting `cross-env NODE_OPTIONS='--inspect=0.0.0.0:5858' ts-node -r tsconfig-paths/register ./src --env=development`
app_1  | Debugger listening on ws://0.0.0.0:5858/k3h42h4-h49d-4f00-adj877-60f6731548787
app_1  | For help, see: https://nodejs.org/en/docs/inspector
app_1  | Service started at ports:3000

Folder structure

App
|-- docker
|   |-- docker-compose.yml
|   |-- Dockerfile
|   `-- .dockerignore
|-- nodemon.json
|-- package.json
|-- tsconfig.json
|-- tslint.json
`-- src
    `-- index.ts

index.ts

import express, { Request, Response } from "express";
const app = express();
const port = process.env.PORT || 3000; // default port to listen

// define a route handler for the default home page
app.get("/", (req: Request, res: Response) => {
  res.send("Hello worlds!");
});

// start the Express server
app.listen(port, () => {
  console.log(`Service started at ports:${port}`);
});

docker-compose.yml

# docker-compose.yml
version: "3"

services:
  app:
    image: app:dev
    build:
      context: ../
      dockerfile: ./docker/Dockerfile
    ports:
      - 3000:3000
    volumes:
      - ../src:/usr/src/app/src/

Dockerfile

FROM node:12-slim

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . ./

CMD [ "npm", "run", "dev:debug" ]

package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "API to receive data",
  "author": "Nikhil Gupta",
  "main": "./dist/index.js",
  "scripts": {
    "dev:debug": "nodemon --config nodemon.json --env=development",
    "dev:docker:debug": "docker-compose -f ./docker/docker-compose.yml up --build"
  },
  "dependencies": {
    "cross-env": "^6.0.3",
    "express": "^4.17.1",
    "ts-node": "^8.5.4",
    "tsconfig-paths": "^3.9.0",
    "tslib": "^1.10.0"
  },
  "devDependencies": {
    "@types/express": "^4.17.2",
    "@types/node": "^12.12.21",
    "nodemon": "^2.0.2",
    "tslint": "^5.20.1",
    "typescript": "^3.7.4"
  }
}

nodemon.json

{
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["src/public"],
  "inspect": true,
  "exec": "cross-env NODE_OPTIONS='--inspect=0.0.0.0:5858' ts-node -r tsconfig-paths/register ./src"
}

Solution

  • As mentioned in the comments by @ykit9 I did following

    1) Added port 5858 into my docker-compose.yml

    # docker-compose.yml
    version: "3"
    
    services:
      app:
        image: app:dev
        build:
          context: ../
          dockerfile: ./docker/Dockerfile
        ports:
          - 3000:3000
          - 5858:5858
        volumes:
          - ../src:/usr/src/app/src/
    

    2) Added following configuration in VS Code's launch.json file.

    {
        "type": "node",
        "request": "attach",
        "name": "Attach to Docker",
        "protocol": "auto",
        "port": 5858,
        "restart": true,
        "localRoot": "${workspaceFolder}/src",
        "remoteRoot": "/usr/src/app/src"
    }