Home > Mobile >  Debug the nestjs project in vscode using docker does not work
Debug the nestjs project in vscode using docker does not work

Time:01-13

I have a NestJs project using docker and I would like to debug it in VSCode.

In package.json, I have: "start:debug": "nest start --debug 0.0.0.0:9229 --watch"

In docker-compose, I have:

version: '3.8'

services:
  core-database:
    image: postgis/postgis:latest
    volumes:
      - /tmp/rg-core/postgres/data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=***
      - POSTGRES_USER=***
      - POSTGRES_PASSWORD=***
    ports:
      - 5433:5432
  core-service:
    container_name: core-service
    working_dir: /usr/src/app
    build:
      context: .
      dockerfile: ./Dockerfile.dev

    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules

    ports:
      - 9001:9001
      #debugging port
      - 9229:9229

      # Database
      - DB_HOST=***
      - DB_PORT=5432
      - DB_NAME=***
      - DB_USER=***
      - DB_PASSWORD=***
    
    command: 'yarn run start:debug'

In launch.json, I have:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach",
            "port": 9229,
            "request": "attach",
            "localRoot": "${workspaceFolder}",
            "type": "node",
            "address": "0.0.0.0",
            "restart": true,
            "remoteRoot": "/usr/src/app",
            "sourceMaps": true,
            "skipFiles": [
                "<node_internals>/**"
            ]
        }
    ]
}

I'm running docker compose up and giving start debugging in VSCode, but it's not stopping at the breakpoint.

Am I doing something wrong? how should it be done?

CodePudding user response:

Was missing port 9229 export in Dockerfile.

In the package.json: "dev": "nest start --debug 0.0.0.0:9229 --watch"

In the docker-compose:

version: '3.8'

services:
  core-database:
    image: postgis/postgis:latest
    volumes:
      - /tmp/rg-core/postgres/data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=***
      - POSTGRES_USER=***
      - POSTGRES_PASSWORD=***
    ports:
      - 5433:5432
  core-service:
    container_name: core-service
    working_dir: /usr/src/app
    build:
      context: .
      dockerfile: ./Dockerfile.dev

    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules

    ports:
      - 9001:9001
      #debugging port
      - 9229:9229

      # Database
      - DB_HOST=***
      - DB_PORT=5432
      - DB_NAME=***
      - DB_USER=***
      - DB_PASSWORD=***
    
    command: 'yarn run dev'

In the launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug core-service",
      "type": "node",
      "request": "attach",
      "remoteRoot": "/usr/src/app",
      "port": 9229,
      "restart": true
    }
  ]
}

In the Dockerfile:

...

EXPOSE 9001 9229

CodePudding user response:

I am trying to deal with the same problem for a few days now.

From my research, VS Code Debug Console by default is listening for console.logs. However NestJS Logger is logging to stdout/stderr (process.stdout.write), so launch.json needs the additional line:

{
  ... ,
  "outputCapture": "std"
}

Sadly, it is still not enough to make it work.

To workaround this for now, I'm attaching a running container's terminal to VS Code integrated terminal manually, executing docker container attach --no-stdin <container-name>.

This is still not a real solution so I hope someone brings one. Thank you for raising this issue. Can you share the contents of your Dockerfile for full reference?

  • Related