1

I have an issue that I am investigating in a Java application that is hosted in a Docker service. The application is a REST API that has it's own docker service

I have added extra logging and looked into the service logs as well as exec-ing into the container and looking at logs there, but that only gets me to a certain point.

Is there any way to attach a remote debugger, or something similar so that I can step through the code of the application?

colmulhall
  • 1,548
  • 3
  • 18
  • 33
  • 1
    Look into https://stackoverflow.com/questions/975271/remote-debugging-a-java-application but note that you shouldn’t do this in production – Roy Shahaf Sep 11 '18 at 10:59
  • Depending on the container you are using you should change the command started by the container. Usually you can add parameters passing the JAVA_OPT environment variable. Can you provide some more info about the container? – Gianluca Mereu Sep 11 '18 at 13:00
  • @GianlucaMereu My application is deployed using a docker compose file with three different services defined. One of these services is the one which I would like to debug. – colmulhall Sep 11 '18 at 14:54
  • Check this link for detailed steps Visit ? – Rachangouda Patil May 07 '20 at 14:18

2 Answers2

4

Dockerfile e.g.:

FROM openjdk:11.0.1-jdk
VOLUME /tmp
COPY build/libs/*.jar app.jar
EXPOSE 5005
EXPOSE 8080
ENTRYPOINT ["java", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005", "-Dspring.profiles.active=localdocker","-jar","/app.jar"]

then connect by remote debugger from Idea or Eclipse

Tomáš Mika
  • 210
  • 1
  • 13
1

You can also leave your docker file the same if you do something like:

VOLUME /tmp
COPY build/libs/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java"]
CMD ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005", "-Dspring.profiles.active=localdocker","-jar","/app.jar"]

And then on the terminal pass the neccessary arguments

docker run -p 5005:5005 <my-container> -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005, -Dspring.profiles.active=localdocker,-jar,/app.jar

This is the documentation for the connection options for jdwp. https://docs.oracle.com/javase/7/docs/technotes/guides/jpda/conninv.html#Transports

TheAppFoundry
  • 93
  • 1
  • 9