0

I am new to docker. I would like to attach debugger to the tests running with gradle inside docker container and debug the tests inside IntelliJ. The docker container is running on my MacBook.

Here's the command for starting a docker container -

docker run \
    --name "my_test_container" \
    -v $(pwd):/root/workspace \
    -e BRANCH_NAME="${BRANCH_NAME}" \
    -p 127.0.0.1:4455:4455/tcp \
    -it "${DOCKER_IMAGE}"

The command to run the tests is -

gradle clean build test --info --stacktrace

I have tried --debug-jvm option in gradle command. I have also included following debugOptions in build.gradle(excluded --debug-jvm).

test {
    // few other options go here
    debugOptions {
        enabled = true
        host = '127.0.0.1'
        port = 4455
        server = true
        suspend = true
    }
}

The execution suspends in the docker container and shows the message in docker terminal

Listening for transport dt_socket at address: 4455

When I start the debug configuration in IntelliJ, I am getting below error in IntelliJ -

enter image description here

IntelliJ debug configuration are as below

enter image description here

Do I need to perform any additional steps such as port forwarding? I am not familiar with networking concepts and wanted to get few pointers from the forum.

References

  1. https://github.com/gradle/gradle/issues/7496
  2. https://medium.com/swlh/remote-debugging-a-java-application-running-in-docker-container-with-intellij-idea-efe54cd77f02
  3. https://www.baeldung.com/docker-debug-app-with-intellij
  4. https://www.neelsomani.com/blog/ssh-tunnel-to-a-docker-container-on-a-remote-server.php
  5. Unable to open debugger port through IntelliJ
M1017108
  • 397
  • 5
  • 14
  • What OS do you use? Can you telnet to 127.0.0.1:4455? Can you check with lsof/netstat for the actual port/interface it's listening on? Also note that Gradle may fork your tests into another JVM so you may be connecting to the wrong process/JVM. – CrazyCoder Mar 10 '23 at 15:40
  • Hi @CrazyCoder, I am on MacOS - 13.2.1. The fact that I see the gradle command inside container suspends and waits for debugger to attach, tells me that the forked process is using the JVM options I specified in build.gradle. – M1017108 Mar 10 '23 at 15:55
  • How do you know it's a forked process with your code and not the Gradle JVM itself? The issue may be also related to localhost resolving to ipv6 instead of ipv4 or vice versa. So check the actual interface it binds to and verify with telnet if the connection works. You can also try command like `jdb`. – CrazyCoder Mar 10 '23 at 15:57
  • Hmm.. Ok. Let me try the things you suggested above. Thanks. – M1017108 Mar 10 '23 at 16:08

1 Answers1

1

How do you know it's a forked process with your code and not the Gradle JVM itself?

Above comment helped to debug the issue.

Step 1 - Run docker container using -p flag.

docker run \
    --name ${TEST_CONTAINER_NAME} \
    -v $(pwd):/root/workspace \
    -e BRANCH_NAME="${BRANCH_NAME}" \
    -p 4455:4455 \
    -it "${DOCKER_IMAGE}"

Step 2 - In my build.gradle, I added jvm args as below.

test {
    useTestNG() {
        suites "./src/test/resources/suites/${suiteFile}"
    }
    jvmArgs += [ "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:4455" ]
    ignoreFailures = true
    systemProperties = System.properties
}

Step 3 - Then created remote debug configs in IntelliJ as below enter image description here

Step 4 - Ran the tests using gradle clean build.... Once the execution was suspended, I attached the debugger in IntelliJ.

NOTE: I didn't have to open any port forwarding via ssh.

M1017108
  • 397
  • 5
  • 14