【问题标题】:Can't connect to Node.js/Jest inspector on exposed Docker container port无法连接到暴露的 Docker 容器端口上的 Node.js/Jest 检查器
【发布时间】:2020-04-18 00:27:05
【问题描述】:

无法连接到 Node.js Chrome 检查器上的 Jest 检查器(端口 9230)

当我跑步时: docker exec -it test_api_run_eb9ed32c928a wget -qO- http://localhost:9230/json

JSON 从容器返回:

[ {
  "description": "node.js instance",
  "devtoolsFrontendUrl": "chrome-devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=localhost:9230/a0dab38a-e6d8-43a3-a459-e3ea9eabf470",
  "devtoolsFrontendUrlCompat": "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=localhost:9230/a0dab38a-e6d8-43a3-a459-e3ea9eabf470",
  "faviconUrl": "https://nodejs.org/static/favicon.ico",
  "id": "a0dab38a-e6d8-43a3-a459-e3ea9eabf470",
  "title": "node_modules/.bin/jest",
  "type": "node",
  "url": "file:///usr/src/sand/node_modules/jest/bin/jest.js",
  "webSocketDebuggerUrl": "ws://localhost:9230/a0dab38a-e6d8-43a3-a459-e3ea9eabf470"
} ]
  • 但是当我访问 chrome://inspect 时,资源并没有列出。我已经添加了localhost:9230 作为发现的网络目标。

  • 当我在浏览器中访问 chrome-devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=localhost:9230/a0dab38a-e6d8-43a3-a459-e3ea9eabf470 时,Chrome 出现以下错误:

  • Debugging connection was closed. Reason: WebSocket disconnected

Docker 端口

运行 docker port test_api_run_eb9ed32c928a 9230 返回: Error: No public port '9230/tcp' published for test_api_run_eb9ed32c928a

Docker 撰写片段:

    expose:
      - '8000'
    ports:
      - '127.0.0.1:9230:9230'

  • 我也尝试在此 compose 上从 ports: 中删除 127.0.0.1:

Node.js / Jest 启动命令:

node --inspect-brk=0.0.0.0:9230 node_modules/.bin/jest --forceExit --runInBand

CLI 输出: Debugger listening on ws://0.0.0.0:9230/74d22b62-051d-4aa3-b312-5f4defb170e6

在容器中运行netstat -a

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:9230            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.11:41339        0.0.0.0:*               LISTEN
netstat: /proc/net/tcp6: No such file or directory
udp        0      0 127.0.0.11:53289        0.0.0.0:*
netstat: /proc/net/udp6: No such file or directory
netstat: /proc/net/raw6: No such file or directory
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path

Docker 检查容器(NetworkSettings sn-p):

        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "5c1fb6ad8189e4407cf40eb8dc0d9f0f8eacc13df6bb0b45b8d48d51da33655f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "9230/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/5c1fb6ad8189",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "test_sand": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "51117364a172"
                    ],
                    "NetworkID": "338c689e41c4ecbb691626560f6f98a04f6d9a93e640a766d37faa773e5ce760",
                    "EndpointID": "0c161fd96b88a60ea10a85d81aeda09cd936added4363dd7ba6613dea3ad9045",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:04",
                    "DriverOpts": null
                }
            }
        }

看起来有问题的区域是:

"Ports": {
    "9230/tcp": null
},
  • Netstat 输出看起来也很奇怪

是否缺少我要解决的步骤,或者端口配置错误,或其他干扰网络的东西?

【问题讨论】:

  • 您能否在docker-compose.yml sn-p 周围添加更多上下文?这也是我认为有问题的部分,它通常意味着您已暴露端口但未发布(ports: 声明不适用)。
  • @DavidMaze :我现在已经添加了完整的 docker compose,谢谢
  • @DavidMaze 现在通过在 docker-compose 运行中包含 --service-ports 来修复它

标签: node.js docker jestjs


【解决方案1】:

通过在 docker-compose 运行中包含 --service-ports 来解决此问题

【讨论】:

    猜你喜欢
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多