将Eclipse连接到Docker容器以进行远程调试

7

我正在尝试将eclipse连接到一个正在运行的docker容器,但我遇到了麻烦。

我的docker运行命令如下:

docker run --rm -it -p 8000:8000 -p=8668:8080 -p 8010:8009 -p 8443:8443 \
--name myContainer -h theContainer -e JVM_ROUTE=myContainer1 myContainer:qa

在eclipse中,我将localhost作为主机连接,将8000作为端口。我进入Run->Debug Configurations->Remote Java Application,并创建了一个新的调试配置。
当我点击应用程序,然后进行调试时,弹出错误消息无法连接到远程VM。 我需要做什么以使远程调试正常工作?

请参阅 https://dev59.com/B3VC5IYBdhLWcg3w9GA9。 - Raedwald
3个回答

4
在Docker容器中运行的Java应用程序可以通过远程调试进行调试。
  1. Enabling JDWP for the java process in the container, e.g.

    java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y [...]
    

    or using the JAVA_OPTS environment variable

    JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y"
    

    Note that suspend=y will prevent the application from starting until a remote debugger is attached to the JVM. If suspend=n is used, the application will start as normal allowing a remote debugger to connect at a later time.

  2. Connecting to the process, e.g. through your IDE, using the port specified in the address=<port> settings above, and importantly the ip address of the docker host which, unless you are running on linux, is probably not localhost. If you are using docker-machine, the docker host ip can be displayed using docker-machine ip, e.g.

    $ docker-machine ip
    192.168.99.100
    

我正在使用Mac电脑。我现在会尝试一下。 - aCarella

2

操作系统: Ubuntu 18 / Windows 10

Java版本: OpenJdk 12

Docker容器: Sprint Boot应用程序

要在Eclipse中连接远程调试,您可以按照以下步骤进行:

  1. 将以下行放入应用程序的Dockerfile中

    # 对于Windows机器,请注释掉EXPOSE 7074并将其添加到docker-compose.yml中
    EXPOSE 7074
    ENV DEBUG_INFO="-Xdebug -Xrunjdwp:transport=dt_socket,address=0.0.0.0:7074,server=y,suspend=n"
    ENTRYPOINT [ "sh", "-c", "java ${DEBUG_INFO} -Dspring.profiles.active=docker -jar /pharmacy-service.jar" ]

对于Windows,请在docker-compose.yml中添加端口


  bank-service:
    image: ....
    environment:
       ...
    ports:
      - 9097:9097
      - 7074:7074

  1. 仅适用于Linux,请启动您的Docker应用程序并搜索网络,在我的情况下为ecs-core_default
    $ docker network ls
    NETWORK ID          NAME                DRIVER              SCOPE
    e63bb0decc92        bridge              bridge              local
    94aefcdbb5f3        ecs-core_default    bridge              local
  1. 仅适用于Linux,请使用以下命令检查应用程序的IP地址:


    $ docker network inspect ecs-core_default
    [
        {
            "Name": "ecs-core_default",
            .....
            "IPAM": {
                "Driver": "default",
                "Options": null,
                "Config": [
                    {
                        "Subnet": "172.18.0.0/16",
                        "Gateway": "172.18.0.1"
                    }
                ]
            },
            .....
            "Containers": {
                "29bebdc31d6bf2057ed31074407c780cc718396ca49f58e766e098fceaa41a41": {
                    "Name": "ecs-core_pharmacy-service_1",
                    "EndpointID": "fadc9b40bfed1d4b2104b96fb6930bda47928256092c268aa4cb67407c2c1661",
                    "MacAddress": "02:42:ac:12:00:06",
                    "IPv4Address": "172.18.0.6/16",
                    "IPv6Address": ""
                }
            }
            .....
        }
    ]


  1. 仅适用于Linux,从容器中复制IP地址:“IPv4Address”:“172.18.0.6/16”,即 172.18.0.6

  2. 仅适用于Windows 10,找到IP地址的方法是:控制面板 -> 网络和Internet -> 查看网络状态和任务 -> 更改适配器设置 -> 找到vEthernet。打开属性,转到网络选项卡,选择TCP / IPv4,然后单击属性按钮并复制IP。 Windows Docker IPv4

  3. 在Eclipse中,运行 -> 调试配置,使用IP地址(截图显示Linux的IPv4,对于Windows将为172.26.48.1)和公开端口(即7074)。 enter image description here

享受吧!


1
这个问题可以通过将localhost替换为我的实际IP地址来解决。

我使用的是Mac电脑,因此我不使用docker-machine - aCarella
你的意思是我没有使用 docker-machine,这个问题与 docker-machine 有关,而你发布了错误的答案!这是相关的! - Dupinder Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接