[英]How to debug Nodejs app running inside Docker container via Google Cloud
I have found Google provides some guidelines on how to run Nodejs on a custom runtime environment . 我发现Google提供了一些有关如何在自定义运行时环境中运行Nodej的指南。 Everything seems fine and I am managing to start my Nodejs app on local machine running
gcloud preview app run .
一切似乎都很好,我正在设法在运行
gcloud preview app run .
本地计算机上启动我的Nodejs应用gcloud preview app run .
. 。
As I can see, it probably creates a Docker container and runs Nodejs program in there. 正如我所看到的,它可能会创建一个Docker容器并在那里运行Nodejs程序。 I am saying "probably" , because it is my first experience with Docker, however I am 2+ years experienced Nodejs developer.
我说“可能” ,因为这是我第一次使用Docker,但我有2年以上经验的Nodejs开发人员。
So my question is how to debug (with breakpoint stops) my Nodejs program when it is running inside Docker container? 所以我的问题是如何调试(使用断点停止)我的Nodejs程序在Docker容器中运行时? Using Chrome Developer Tools or how can I set up Webstorm debug configuration to make it stop on breakpoints.
使用Chrome开发者工具或如何设置Webstorm调试配置以使其在断点处停止。 Is it possible to configure Docker on how it starts node or even start Docker via
gcloud
inside Webstorm to assure debugging is working? 是否可以配置Docker如何启动节点,甚至可以通过
gcloud
gcloud启动Docker以确保调试正常? Any help or clarifications are appreciated. 任何帮助或澄清表示赞赏。
Please don't provide answers on how to debug Nodejs app outside of Docker container – I know how to do that very well. 请不要提供有关如何在Docker容器之外调试Nodejs应用程序的答案 - 我知道如何做得很好。
I'm sorry, but I only know a solution with node-inspector, I hope it can help you: 对不起,我只知道node-inspector的解决方案,希望它可以帮到你:
Run this inside your container (with docker exec, or docker-enter) 在容器内运行(使用docker exec或docker-enter)
node-debug --web-host 0.0.0.0 yourScript.js
node-debug --web-host 0.0.0.0 yourScript.js
Go to http://localhost:8080/debug?port=5858 转到http:// localhost:8080 / debug?port = 5858
There is an easier way, at least from Docker 0.11 or something. 有一种更简单的方法,至少从Docker 0.11或其他东西。
Run, on your development machine only, Docker with --net="host". 仅在您的开发机器上运行Docker with --net =“host”。 This makes Docker bind to the localhost directly, and not creating a bridging network adapter, so the Docker machine runs like any other process on your machine and opens the ports it needs on the local interface.
这使得Docker直接绑定到本地主机,而不是创建桥接网络适配器,因此Docker计算机的运行方式与计算机上的任何其他进程一样,并在本地接口上打开所需的端口。
This way, you can connect to your debug port as if Node was not running inside Docker. 这样,您就可以连接到调试端口,就像Node未在Docker中运行一样。
More documentation here : https://docs.docker.com/reference/run/ 更多文档: https : //docs.docker.com/reference/run/
Before Docker 0.11 you have other two ways of debugging, apart from using node-inspector : 在Docker 0.11之前,除了使用node-inspector之外,还有其他两种调试方法:
By default, the node debugger will listen only for connections for the same host ( 127.0.0.1
). 默认情况下,节点调试器将仅侦听同一主机(
127.0.0.1
)的连接。 But in Docker, you need to accept connections from any host ( 0.0.0.0
): 但是在Docker中,您需要接受来自任何主机的连接(
0.0.0.0
):
# inside Docker
node --inspect=0.0.0.0:9229 myapp.js
Also you have to expose the debug port (9229). 您还必须公开调试端口(9229)。 Then the application should be automatically detected and listed as a Remote Target in
chrome://inspect/#devices
in Chrome (tested in Chrome 67). 然后应该自动检测应用程序并将其列为Chrome中的
chrome://inspect/#devices
中的远程目标(在Chrome 67中测试)。
Here is a minimal example. 这是一个最小的例子。 It runs a simple JavaScript application in Docker and shows how to attach the Chrome debugger to it:
它在Docker中运行一个简单的JavaScript应用程序,并显示如何将Chrome调试器附加到它:
$ cat example.js
setInterval(() => console.log('Hallo world'), 1000);
$ cat Dockerfile
FROM node
COPY example.js /
CMD node --inspect=0.0.0.0:9229 /example.js
Run with: 运行:
$ docker build . -t myapp && docker run -p 9229:9229 --rm -it myapp
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM node
---> aa3e171e4e95
Step 2/3 : COPY example.js /
---> Using cache
---> 3ef6c0311da2
Step 3/3 : CMD node --inspect=0.0.0.0:9229 /example.js
---> Using cache
---> e760739c2802
Successfully built e760739c2802
Successfully tagged debug-docker:latest
Debugger listening on ws://0.0.0.0:9229/4177f6cc-85e4-44c6-9ba3-5d8e28e1b124
For help see https://nodejs.org/en/docs/inspector
Hallo world
Hallo world
Hallo world
...
Open Chrome and go to chrome://inspect/#devices
. 打开Chrome并转到
chrome://inspect/#devices
。 It should soon after the start of the application, detect it and list it. 它应该在应用程序启动后不久检测并列出它。
For debugging Docker network issues, docker inspect
is useful: 为了调试Docker网络问题,
docker inspect
非常有用:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae83d50e24c8 debug-docker "/bin/sh -c 'node --…" 2 minutes ago Up 2 minutes 0.0.0.0:9229->9229/tcp blissful_sammet
$ docker inspect ae83d50e24c8
...
"NetworkSettings": {
"Bridge": "",
"SandboxID": "682d3ac98b63d4077c5d66a516666b6615327cbea0de8b0a7a2d8caf5995b0ae",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"9229/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "9229"
}
]
},
...
If want to see the requests sent between Docker and Chrome, ngrep can help: 如果想查看Docker和Chrome之间发送的请求, ngrep可以提供帮助:
$ sudo ngrep -d any port 9229
interface: any
filter: (ip or ip6) and ( port 9229 )
############################
T ::1:38366 -> ::1:9229 [AP]
GET /json/version HTTP/1.1..Host: [::1]:9229....
#####
T ::1:38368 -> ::1:9229 [AP]
GET /json HTTP/1.1..Host: [::1]:9229....
##############
T 172.17.0.1:56782 -> 172.17.0.2:9229 [AP]
GET /json HTTP/1.1..Host: [::1]:9229....
#
T 172.17.0.1:56782 -> 172.17.0.2:9229 [AP]
GET /json HTTP/1.1..Host: [::1]:9229....
###
T 172.17.0.1:56784 -> 172.17.0.2:9229 [AP]
GET /json/version HTTP/1.1..Host: [::1]:9229....
#
T 172.17.0.1:56784 -> 172.17.0.2:9229 [AP]
GET /json/version HTTP/1.1..Host: [::1]:9229....
###
T 172.17.0.2:9229 -> 172.17.0.1:56782 [AP]
HTTP/1.0 200 OK..Content-Type: application/json; charset=UTF-8..Cache-Contro
l: no-cache..Content-Length: 465....
#
T 172.17.0.2:9229 -> 172.17.0.1:56782 [AP]
HTTP/1.0 200 OK..Content-Type: application/json; charset=UTF-8..Cache-Contro
l: no-cache..Content-Length: 465....
###
T 172.17.0.2:9229 -> 172.17.0.1:56782 [AP]
[ {. "description": "node.js instance",. "devtoolsFrontendUrl": "chrome-de
vtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=[::
1]:9229/f29686f9-e92d-45f4-b7a2-f198ebfc7a8e",. "faviconUrl": "https://node
js.org/static/favicon.ico",. "id": "f29686f9-e92d-45f4-b7a2-f198ebfc7a8e",.
"title": "/example.js",. "type": "node",. "url": "file:///example.js",.
"webSocketDebuggerUrl": "ws://[::1]:9229/f29686f9-e92d-45f4-b7a2-f198ebfc7a
8e".} ]..
#
As far as I can see, you need to provide the parameter --debug-brk= to node upon startup - this will enable debugging. 据我所知,您需要在启动时向节点提供参数--debug-brk = - 这将启用调试。 After that, access the specified port on your docker container.
之后,访问docker容器上的指定端口。 You probably have to expose it or tunnel (using ssh).
您可能需要公开它或隧道(使用ssh)。
After that, point the Webstorm remote debugger at the specified port, and you should be set. 之后,将Webstorm远程调试器指向指定的端口,您应该进行设置。
If you are using bridge networking for your containers, and you don't want to install node-inspector inside the same container as your node process, I've found this to be a convenient solution: 如果您正在为容器使用桥接网络,并且您不想在节点进程的同一容器中安装node-inspector,我发现这是一个方便的解决方案:
This say, the node-inspector container will connect to localhost:5858 which will then be port mapped through to the main node container. 这就是说,node-inspector容器将连接到localhost:5858,然后将其端口映射到主节点容器。
If you're running this on a public VM, I'd then recommend: 如果你在公共虚拟机上运行它,我会建议:
I wrote a few more details about it here: https://keylocation.sg/our-tech/debugging-nodejs-in-docker-using-node-inspector 我在这里写了一些关于它的细节: https : //keylocation.sg/our-tech/debugging-nodejs-in-docker-using-node-inspector
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.