4

Goal

My goal is to be able to debug multiple Node.js services running in docker-compose with the help of VSCode. But there are some challenges involved:

  1. ts-node-dev has no documentation for VSCode debugging. But since it is just a wrapper around ts-node (which has native support for VSCode dbugging) this should be possible with something like described here. Or do I have to use ts-node when I want to debug?

  2. How to debug the code that is running inside a Docker container? For that purpose I might do something similar as described here. But they compile the TYpescript to Javascript manually, which I don't want to do.

  3. How to coordinate multiple services? Because I have multiple services, would I have to choose which one I want to debug or is it possible to launch the debugger for all of the services at once?

The project

I've made a small sample project with the following structure. There are two services (gateway and hello) running in docker-compose.

package.json
tsconfig.json
docker-compose.yaml
services
  hello
    index.ts
  gateway
    index.ts

After running docker-compose up --build:

Here is the docker-compose.yaml file I use to start the project in development mode.

version: "3"
services:
  gateway:
    image: node:lts-alpine
    working_dir: /
    volumes:
      - ./package.json:/package.json
      - ./tsconfig.json:/tsconfig.json
      - ./services/gateway:/services/gateway
      - ./node_modules:/node_modules
    command: yarn run ts-node-dev services/gateway
    ports:
      - 3000:3000

  hello:
    image: node:lts-alpine
    working_dir: /
    volumes:
      - ./package.json:/package.json
      - ./tsconfig.json:/tsconfig.json
      - ./services/hello:/services/hello
      - ./node_modules:/node_modules
    command: yarn run ts-node-dev services/hello

Note that I use ts-node-dev to run the services, which automatically restarts the servers when a change in the code as detected, but I would be willing to use something else if necessary.

Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137

1 Answers1

0

I've made a video explaining how to make this work.

https://odysee.com/@flolu:7/docker-typescript-debug:3?r=HFBL1PfaSVqKsaYbQDRH3GZZp78ch6CR

And this is the source code: https://github.com/flolu/docker-typescript-debug

Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137