为什么Chrome无法检查Docker容器中的nodejs代码? [英] Why Chrome can't inspect nodejs code in Docker container?

查看:106
本文介绍了为什么Chrome无法检查Docker容器中的nodejs代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Docker容器中启动简单的nodejs服务器,并使用chrome:// inspect或WebStorm对其进行调试。调试端口9229已绑定,但检查不起作用。另一方面,当我在没有docker的情况下运行相同的代码时,我可以在chrome:// inspect和WebStorm中很好地对其进行检查。

I try to start simple nodejs server inside Docker container and debug it with chrome://inspect or WebStorm. Debugging port 9229 is binded but inspection not works. On the other hand when I run same code without docker i can inspect it in chrome://inspect and in WebStorm both well.

有人可以向我解释为什么Chrome可以吗? t检查Docker容器中的nodejs代码吗?

Can anybody explain me why Chrome can't inspect nodejs code in Docker container???

Dockerfile

FROM node:8.2.1-alpine

WORKDIR /code

COPY package.json /code/package.json
RUN npm install && npm ls
RUN mv /code/node_modules /node_modules

COPY . /code

EXPOSE 8000
EXPOSE 9229

CMD ["npm", "run", "start"]

alexey @ home:〜/ app $ docker run -p 9229:9229 -p 8000:8000 node-dev

npm info it worked if it ends with ok 
npm info using npm@5.3.0  
npm info using node@v8.2.1  
npm info lifecycle @~prestart: @  
npm info lifecycle @~start: @

> @ start /code
> node --inspect app

Debugger listening on ws://127.0.0.1:9229/5b225f7d-0e18-4ded-894b-a7993bb7da64 
For help see https://nodejs.org/en/docs/inspector 
HTTP server listening on port 8000


推荐答案

在您的package.json 脚本

in your package.json scripts:

"debug": "nodemon --inspect=0.0.0.0:9229 index.js",

-compose.yaml:

in your docker-compose.yaml:

services:
  service_name:
    command: npm run debug
    ports:
      - 9229:9229

对此我不是100%肯定,但我认为将该调试器映射为在0.0.0.0上运行将其暴露于您的本地网络,这意味着任何人都可以在端口9229上连接到您的计算机IP上,从而可以调试您的nodejs服务器。请注意,您的nodejs服务器有权访问文件系统。因此,请勿以此来运行生产(或开发)服务器。

I am not 100% sure on this but I think that mapping the debugger to run on 0.0.0.0 exposes it to your local network meaning that anyone can connect to your machine IP on port 9229 will be able to debug your nodejs server. Beware your nodejs server has access to the filesystem. So do not run production (or development) servers with this ever.

这篇关于为什么Chrome无法检查Docker容器中的nodejs代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆