'Remote Debugging of Tomcat/Wildfly server in docker container in VSCode/Intellij

I want to remote debug a Java application in Wildfly/Tomcat embedded in a Docker container. I want to debug the application in VsCode; I also tried to remote debug in IntelliJ. For both I am getting the same error:

Error running 'Tomcat in Docker': Unable to open debugger port (localhost:9000): java.net.SocketException "Connection reset"

docker-compose

version: '3.7'

services:
   wildfly:
      image: jboss/wildfly:latest
      ports:
        - 8088:8080
        - 9990:9990
        - 8787:8787
      entrypoint: "/opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 --debug"
   tomcat:
     image: tomcat:10
     environment:
       - JPDA_ADDRESS=8000
       - JPDA_TRANSPORT=dt_socket
     ports:
       - 8888:8080
       - 9000:8000
     entrypoint: "/usr/local/tomcat/bin/catalina.sh jpda run"
  

launch.json in VsCode


{
    "configurations": [
      {
        "type": "java",
        "name": "Attach Wildfly",
        "request": "attach",
        "hostName": "localhost",
        "port": "8787"
      }, 
      {
        "type": "java",
        "name": "Attach Tomcat",
        "request": "attach",
        "hostName": "localhost",
        "port": "9000"
      }
  ]
}
netstat -tuplen | grep 8787

tcp        0      0 0.0.0.0:8787            0.0.0.0:*               LISTEN      0          257375     -                   
tcp6       0      0 :::8787                 :::*                    LISTEN      0          257380     - 
netstat -tuplen | grep 9000
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      0          255587     -                   
tcp6       0      0 :::9000                 :::*                    LISTEN      0          253926     - 

Any ideas? Thanks.



Solution 1:[1]

I had a similar issue when trying to attach to a tomcat container. VS Code displayed the following message:

Failed to connect to remote VM com.sun.jdi.connect.spi.ClosedConnectionException

I think the problem is that the process is not allowing external connection unless it is listening on 0.0.0.0.

Add this environment variable to your tomcat container in your docker compose file:

JPDA_OPTS: "-agentlib:jdwp=transport=dt_socket,address=0.0.0.0:8000,server=y,suspend=n"

Alternatively, you can use the JAVA_TOOL_OPTS to pass the variables, but then you should start catalina regularly (without the jpda).

JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,address=0.0.0.0:8000,server=y,suspend=n"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohamed