go delve 远程调试在使用 Docker 的 “network_mode: host” 时无法工作。

huangapple go评论77阅读模式
英文:

go delve remote debugging does not work with docker "network_mode: host"

问题

嗨,我正在尝试在使用VSCode的Docker容器中使用dlv进行远程调试。
问题出现在我使用docker-compose标志network_mode: host而不是指定特定端口时(由于使用MQTT而需要这样做)。这会导致VSCode抛出以下错误:"Failed to continue: "Error: connect ENCONNREFUSED ...""

.vscode/launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": "Remote Docker",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath":"/go/src/work/cmd/mapper",
"port": 2345,
// "host": "127.0.0.1",
"cwd": "${workspaceFolder}/src/cmd/mapper",
"args": [],
"trace" : "verbose",
"env" : {}
},
]
}

docker-compose.yml

version: "3.4"
services:
golang:
container_name: golang
image: gotestdlv:latest
privileged: true
volumes:
- ./src/:/go/src/work/
network_mode: host
# ports:
# - 2345:2345 # debug port

英文:

Hi I am trying to run remote debugging using dlv inside a docker container with VSCode.
Problem starts when I specify docker-compose flag network_mode: host instead of the specific port (This is required due to use of MQTT). This causes VSCode to throw to the following error: "Failed to continue: "Error: connect ENCONNREFUSED ...""

.vscode/launch.json

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Remote Docker",
        "type": "go",
        "request": "attach",
        "mode": "remote",
        "remotePath":"/go/src/work/cmd/mapper",
        "port": 2345,
        // "host": "127.0.0.1",
        "cwd": "${workspaceFolder}/src/cmd/mapper",
        "args": [],
        "trace" : "verbose",
        "env" : {}
    },
]
}

docker-compose.yml

version: "3.4"
services:
  golang:
    container_name: golang
    image: gotestdlv:latest
    privileged: true
    volumes:
      - ./src/:/go/src/work/
    network_mode: host
    # ports:
      # - 2345:2345 # debug port

答案1

得分: 1

有很多原因可能导致它对你不起作用。其中包括:

  • 主机网络驱动程序仅适用于Linux主机,因此如果你使用的是Docker for Mac、Docker for Windows或Docker Enterprise Edition,它将无法工作。
  • 端口2345可能已经在你的机器上被占用,delve进程可能无法绑定它。
  • 当连接到会话时,你可能使用了错误的主机名(你的代码片段显示了127.0.0.1,但它被注释掉了,我不知道在没有提供IP时它选择了什么IP)。

然而,我不确定你为什么要尝试使用主机网络模式,因为通常它只适用于两种主要用例:当Docker容器需要绑定大量端口(数百或数千个端口)时,使用主机网络模式性能更好;当运行Docker集群时,只要服务在集群中没有冲突的端口,主机网络模式也很方便。

在我看来,似乎没有任何与你描述的情况相关的情况,所以尽管这不是你最初的问题,但我建议你寻找其他解决方案来解决你尝试使用主机网络模式解决的问题。

英文:

There can be a multitude of reasons why it does not work for you. Among them are:

  • The host networking driver only works on Linux hosts, so if you use Docker for Mac, Docker for Windows or Docker Enterprise Edition it won't work.
  • The port 2345 might be already in use on your machine, and the delve process might not be able to bind it.
  • You might be using the wrong hostname when connecting to your session (your snippet shows 127.0.0.1 but it is commented out, and I don't know what IP it selects when one is not provided).

I am not sure why you are trying to use the host networking mode however, as it is normally useful for two main use-cases: it is more performant to use it when a docker container needs to bind a large range of ports (hundreds or thousands of ports), and can be convenient when running a Docker swarm as long as services do not have conflicting ports within the swarm.

It doesn't seem to me like any of those cases are related to the situation you describe, so even though that was not your original question, it is my recommendation that you look for other solutions to solve the problem which you attempted to solve by using host networking mode.

huangapple
  • 本文由 发表于 2022年1月7日 17:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/70619511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定