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.

3
  • just a thought, have you tried VSCode remote containers extension code.visualstudio.com/docs/remote/containers ? Commented Apr 27, 2020 at 7:12
  • @V.i.K.i looks interesting. But the problem I see at first glance is that it runs inside one container. But I have many containers. But I will take a closer look at it :) Commented Apr 27, 2020 at 7:34
  • Sure. Give it a try. Commented Apr 27, 2020 at 8:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.