Search code examples
dockerdebuggingdocker-composedeno

deno inspect with docker compose


I am trying to inspect a deno app that is ran inside a docker container with docker-compose.

docker-compose configuration is as follows:

services:
  api_bo:
    image: denoland/deno:debian-1.23.4
    volumes:
      - type: bind
        source: .
        target: /usr/src
    ports:
      - 9229:9229
      - 6005:3000
    command: bash -c "cd /usr/src/packages/api_bo && deno task inspect"
    depends_on:
      - mongo_db
    environment:
      - MONGO_URL=mongodb://mongo_db:27017/academy_db
      - DB_NAME=academy_db
      - PORT=3000

deno.json is as follows:

{
  "compilerOptions": {
    "allowJs": false,
    "strict": true
  },
  "lint": {
    "files": {
      "include": ["src/"],
      "exclude": ["src/types.ts"]
    },
    "rules": {
      "tags": ["recommended"],
      "include": [
        "ban-untagged-todo",
        "no-explicit-any",
        "no-implicit-any",
        "explicit-function-return-type"
      ],
      "exclude": ["no-debugger", "no-console"]
    }
  },
  "tasks": {
    "start": "deno run -A --watch src/app.ts",
    "inspect": "deno run -A --inspect src/app.ts"
  },
  "importMap": "../../import_map.json"
}

Chrome with chrome://inspect does not detect the running process.

When running out of docker with deno run, it works just fine.

It seems that deno only listens to connections from 0.0.0.0 and thus it cannot be inspected using docker port forwarding.


Solution

  • Deno and NodeJS share the same Inspector Protocol from V8, for more see V8 / Docs / Inspector.

    And also (lucky) the same parameter "--inspect=[HOST:PORT]" and "--inspect-brk=[HOST:PORT]" so on, for more see NodeJS / API / Inspect or NodeJS / API / Inspect Brk (THIS is the documentation from NODEJS, so be careful!)

    The main "problem" (security reasons) is that NodeJS and Deno Inspector Protocol listen only to localhost / 127.0.0.1 and Docker can't and won't forward these ports. But with the parameter "--inspect" you can change the Host and Port.

    Deno CLI

    deno run --inspect=0.0.0.0:9229 ./src/my-big-cool-file.ts
    

    Dockerfile

    # ...
    EXPOSE 9229
    # ...
    CMD ["run", "--inspect=0.0.0.0:9229", "...", "./src/main.ts"]
    

    TLDR;

    your deno.json

    //...
      "tasks": {
        "start": "deno run -A --watch src/app.ts",
        "inspect": "deno run -A --inspect=0.0.0.0:9229 src/app.ts"
      },
    //...
    

    your docker-compose.yml

    services:
      # ...
      api_bo:
        # ...
        ports:
          # THIS IS IMPORTANT, FORWARD DEBUG PROTOCOLS ONLY TO YOUR LOCALHOST! **1
          - 127.0.0.1:9229:9229
    

    **1 except: when you need it, and you know what you are doing!!!