docs icon indicating copy to clipboard operation
docs copied to clipboard

Debug port not exposed when using jvmArguments in Dockerfile CMD

Open nlecaros opened this issue 1 year ago • 0 comments

Is this a docs issue?

  • [X] My issue is about the documentation content or website

Type of issue

Information is incorrect

Description

Using JDK 17 and following the instructions in the section Dockerfile for development, this statement in Dockerfile does not cause the JVM to listen on port 8000:

CMD [ "java", "-Dspring.profiles.active=postgres", "-Dspring-boot.run.jvmArguments='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000'", "org.springframework.boot.loader.launch.JarLauncher"

While trying to attach to that port within the container, I get a "connection refused.":

docker compose up --build
...
docker exec -ti spring-petclinic-server-1 bash
root@95632c87fcf7:/build# jdb -attach localhost:8000
java.net.ConnectException: Connection refused
	at java.base/sun.nio.ch.Net.connect0(Native Method)
	at java.base/sun.nio.ch.Net.connect(Net.java:579)
	at java.base/sun.nio.ch.Net.connect(Net.java:568)
	at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:593)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
	at java.base/java.net.Socket.connect(Socket.java:633)
	at jdk.jdi/com.sun.tools.jdi.SocketTransportService.attach(SocketTransportService.java:261)
	at jdk.jdi/com.sun.tools.jdi.GenericAttachingConnector.attach(GenericAttachingConnector.java:119)
	at jdk.jdi/com.sun.tools.jdi.SocketAttachingConnector.attach(SocketAttachingConnector.java:83)
	at jdk.jdi/com.sun.tools.example.debug.tty.VMConnection.attachTarget(VMConnection.java:557)
	at jdk.jdi/com.sun.tools.example.debug.tty.VMConnection.open(VMConnection.java:367)
	at jdk.jdi/com.sun.tools.example.debug.tty.Env.init(Env.java:63)
	at jdk.jdi/com.sun.tools.example.debug.tty.TTY.main(TTY.java:1113)

Fatal error:
Unable to attach to target VM.

While trying to attach to that port outside the container, I get "connection prematurally closed":

$ jdb -attach localhost:8000
java.io.IOException: handshake failed - connection prematurally closed
	at jdk.jdi/com.sun.tools.jdi.SocketTransportService.handshake(SocketTransportService.java:139)
	at jdk.jdi/com.sun.tools.jdi.SocketTransportService.attach(SocketTransportService.java:273)
	at jdk.jdi/com.sun.tools.jdi.GenericAttachingConnector.attach(GenericAttachingConnector.java:119)
	at jdk.jdi/com.sun.tools.jdi.SocketAttachingConnector.attach(SocketAttachingConnector.java:83)
	at jdk.jdi/com.sun.tools.example.debug.tty.VMConnection.attachTarget(VMConnection.java:563)
	at jdk.jdi/com.sun.tools.example.debug.tty.VMConnection.open(VMConnection.java:369)
	at jdk.jdi/com.sun.tools.example.debug.tty.Env.init(Env.java:63)
	at jdk.jdi/com.sun.tools.example.debug.tty.TTY.main(TTY.java:1176)

Fatal error:
Unable to attach to target VM.

The following change to Dockerfile works:

ENV JAVA_TOOL_OPTIONS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000
CMD [ "java", "-Dspring.profiles.active=postgres", "org.springframework.boot.loader.launch.JarLauncher" ]

Location

https://docs.docker.com/language/java/develop/#dockerfile-for-development

Suggestion

In Dockerfile, instead of:

CMD [ "java", "-Dspring.profiles.active=postgres", "-Dspring-boot.run.jvmArguments='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000'", "org.springframework.boot.loader.launch.JarLauncher"

why not replace the above with:

ENV JAVA_TOOL_OPTIONS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000
CMD [ "java", "-Dspring.profiles.active=postgres", "org.springframework.boot.loader.launch.JarLauncher" ]

nlecaros avatar May 27 '24 02:05 nlecaros