简体   繁体   English

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

[英]Connecting Eclipse to Docker Container for Remote Debugging

I'm trying to connect eclipse to a docker container I have running but I am having trouble.我正在尝试将 eclipse 连接到我正在运行的 docker 容器,但我遇到了问题。

My docker run command is as follows:我的 docker run 命令如下:

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

In eclipse, I'm connecting with localhost as the host, and 8000 as the port.在 eclipse 中,我连接localhost作为主机, 8000作为端口。 I go to Run->Debug Configurations->Remote Java Application, and I've created a new debug configuration.我转到运行-> 调试配置-> 远程 Java 应用程序,并创建了一个新的调试配置。

我的项目调试

When I click apply, then debug, I get a pop up error message Failed to connect to remote VM.当我单击应用,然后调试时,我收到一条弹出错误消息Failed to connect to remote VM.

无法连接

What else do I need to do to get remote debugging working properly?我还需要做什么才能使远程调试正常工作?

A java application running in a docker container can be remotely debugged by运行在 docker 容器中的 java 应用程序可以通过以下方式远程调试

  1. Enabling JDWP for the java process in the container, eg为容器中的java进程启用JDWP ,例如

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

    or using the JAVA_OPTS environment variable或使用 JAVA_OPTS 环境变量

    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.请注意, suspend=y将阻止应用程序启动,直到远程调试器连接到 JVM。 If suspend=n is used, the application will start as normal allowing a remote debugger to connect at a later time.如果使用suspend=n ,应用程序将正常启动,允许远程调试器稍后连接。

  2. Connecting to the process, eg 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 .连接到进程,例如通过您的 IDE,使用在上面的address=<port>设置中指定的address=<port> ,重要的是docker 主机的 ip 地址,除非您在 linux 上运行,否则可能不是localhost If you are using docker-machine , the docker host ip can be displayed using docker-machine ip , eg如果您使用的是docker-machine ,则可以使用docker-machine ip显示 docker 主机docker-machine ip ,例如

    $ docker-machine ip 192.168.99.100

OS: Ubuntu 18 / Windows 10操作系统: Ubuntu 18 / Windows 10

Java: OpenJdk 12 Java: OpenJdk 12

Docker Container: Sprint boot application Docker 容器: Sprint 启动应用程序

To connect Remote Debug in Eclipse you can follow these steps:要在 Eclipse 中连接远程调试,您可以按照以下步骤操作:

  1. Put these lines in your application Dockerfile将这些行放在您的应用程序 Dockerfile 中

    # For Windows Machine comment EXPOSE 7074 and add it to 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" ]

For Windows, add ports in docker-compose.yml对于 Windows,在 docker-compose.yml 中添加端口


  bank-service:
    image: ....
    environment:
       ...
    ports:
      - 9097:9097
      - 7074:7074
  1. For Linux only, bring your docker application up and search for network, in my case ecs-core_default仅适用于 Linux,启动您的 docker 应用程序并搜索网络,在我的情况下为ecs-core_default
$ docker network ls
    NETWORK ID          NAME                DRIVER              SCOPE
    e63bb0decc92        bridge              bridge              local
    94aefcdbb5f3        ecs-core_default    bridge              local
  1. For Linux only, now check IP for your application using,仅适用于 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. For Linux only, copy IP address from Containers "IPv4Address": "172.18.0.6/16" ie 172.18.0.6仅适用于 Linux,从容器“IPv4Address”复制 IP 地址:“172.18.0.6/16”即172.18.0.6

  2. For Windows 10 only, to find IP goto Control Panel -> Network and Internet -> View Network Status and task -> Change adapter settings -> Look for vEthernet.仅对于 Windows 10,要查找 IP,请转到控制面板 -> 网络和 Internet -> 查看网络状态和任务 -> 更改适配器设置 -> 查找 vEthernet。 Open Properties goto Networking tab, select TCP/IPv4 and then click Properties button and copy IP.打开属性转到网络选项卡,选择 TCP/IPv4,然后单击属性按钮并复制 IP。 Windows Docker IPv4

  3. In Eclipse, Run -> Debug Configuration, use IP (screenshot shows IPv4 for Linux, for Windows it will be 172.26.48.1) and exposed port (ie 7074).在 Eclipse 中,运行 -> 调试配置,使用 IP(屏幕截图显示 Linux 的 IPv4,对于 Windows,它将是 172.26.48.1)和暴露的端口(即 7074)。 在此处输入图片说明

Enjoy!!享受!!

这是通过用我的实际 IP 地址替换localhost来解决的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM