Connecting Eclipse to Docker Container for Remote Debugging

11,625

Solution 1

This was solved by replacing localhost with my actual IP address.

Solution 2

A java application running in a docker container can be remotely debugged by

  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
    

Solution 3

OS: Ubuntu 18 / Windows 10

Java: OpenJdk 12

Docker Container: Sprint boot application

To connect Remote Debug in Eclipse you can follow these steps:

  1. Put these lines in your application 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


  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
    $ 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,


    $ 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" i.e. 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. Open Properties goto Networking tab, select TCP/IPv4 and then click Properties button and copy 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 (i.e 7074). enter image description here

Enjoy!!

Share:
11,625
aCarella
Author by

aCarella

I work in logistics, and use databases as well as Office products to make efficient and effective applications for the operation that I am a part of. StackOverflow has really helped me so far, and I will continue to use it as a resource as well as contribute as much as I can to create a better understanding of what I know for other people. user:2664815

Updated on June 28, 2022

Comments

  • aCarella
    aCarella almost 2 years

    I'm trying to connect eclipse to a docker container I have running but I am having trouble.

    My docker run command is as follows:

    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. I go to Run->Debug Configurations->Remote Java Application, and I've created a new debug configuration.

    my project debugging

    When I click apply, then debug, I get a pop up error message Failed to connect to remote VM.

    cant connect

    What else do I need to do to get remote debugging working properly?

  • aCarella
    aCarella about 6 years
    I am on a Mac. I'll give this a try now.
  • aCarella
    aCarella about 6 years
    I'm on a Mac, so I don't use docker-machine.
  • Dupinder Singh
    Dupinder Singh over 4 years
    what do you mean by I am not using docker-machine this issue is related to docker-machine, and you are posting a wrong answer! this is relevant!