Exposing node debugging port in docker container won’t connect? [duplicate]

This question already has an answer here:

I’m trying to connect debugger (chrome) to a node applciation running though docker but something isn’t quiet hooking up and I’m not sure what.

The application itself (to cut a long story short) is using ts-node to host the application. I’ve changed this command to:

node --inspect-brk -r ts-node/register ./src/server.ts

and when I run this (via the package.json command npm run watch) I get a debug hook in chrome:

enter image description here

So that works outside of docker. This application is then containerised and then ran (with various other containers) using docker-compose.

I’ve expose the port in the Dockerfile:

EXPOSE 9229

and I’ve mapped the port in the docker-compose.yaml:

ports: 
- 9229:9229
command: 'npm run watch'

(not sure if I need to do both)

When I run my docker-compose up (after a build) I can see the inspector is running and listening on the correct port:

queries_1           | [nodemon] starting `node --inspect-brk -r ts-node/register ./src/server.ts`
queries_1           | Debugger listening on ws://127.0.0.1:9229/5748f2ad-239b-4ecf-b059-b09828899355
queries_1           | For help, see: https://nodejs.org/en/docs/inspector

I can also see that docker is mapping the port correctly (via a docker ps command):

"npm run watch"          19 seconds ago      Up 16 seconds       3000/tcp, 127.0.0.1:9229->9229/tcp

But the debugger never attaches in chrome.

What am I missing here?

Source: StackOverflow