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:
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?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.
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
:
- http://localhost:3000 - gateway responds directly
- http://localhost:3000/hello - gateway sends response from hello service
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.