April 4, 2019 By Shili Yang 3 min read

Four key steps to configure a remote debugging session for Java Spring Boot microservices running in Docker containers

At the IBM Cloud Garage, we help customers accelerate their digital transformation by modernizing applications for the cloud via our industry-leading IBM Cloud Garage Method. On a recent customer engagement, we built microservices using Java Spring Boot that were deployed as Docker containers hosted on IBM Cloud Private (ICP). Some of the services interact with other components, such as cache or an LDAP user registry. These components are only accessible within a Virtual Private Cloud (VPC) that the service containers are also part of. In other words, they are not reachable from a developer’s workstation that’s outside of the VPC. As a result, it was very difficult for developers to troubleshoot issues locally that required debugging.

Fortunately, we were able to leverage the Java remote debugging support available in Spring Tool Suite (STS) and other Eclipse-based IDEs and debug a service instance inside a container on ICP directly. In this article, we will go over the four key steps to configure a remote debugging session:

  1. Build the service in debug mode.

  2. Modify the Dockerfile to expose the debug port on the Docker image, and start/run the application in debug mode.

  3. Deploy the Docker container with the debugger port exposed.

  4. Run a remote debugging session from STS/Eclipse

Building the service in debug mode

First, compile the service with the additional debug information to enabled debugging at runtime. This is easily done by adding the javac parameter -g in your build script. For example, the maven wrapper mvnw.

Modifying the Dockerfile for building the container image

Next, modify the Dockerfile for building the container image of the service to do the following:

  1. Start the service in debug mode

  2. Expose the debugger port, which STS/Eclipse debugger will connect to, on the Docker image

For the first step there, add two JVM parameters to the service startup command, which, for example, may be defined in entrypoint.sh:

  • -Xdebug: to enable debugging

  • -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n to configure the JDWP protocol for remote debugging (the port 8000 is used as an example).

For #2, add the debugger port defined above (e.g., 8000) to the list of exposed ports for the image with the EXPOSE command.

Running the service container with debugger port exposed

Next, modify the docker run command or the configuration yaml file for container deployment. Deploy the service container with the additional debugger port published to the host.

Also, make sure the port is accessible to your local/desktop environment. For example, on IBM Cloud Private/IBM Cloud Kubernetes Service, use the port-forward command to map the port to a port on a local machine. Now your service is ready for debugging.

Running a remote debugging session from STS/Eclipse

Last, but not least, configure the STS/Eclipse to connect to the service instance:

  • Create a new Remote Java Application Debug Configuration;

  • Select source code project for the service under Project

  • Specify the debugger port defined above under Connection Properties

The configuration is now complete. Start the remote debugging session and the STS will connect to the debugger port inside the container. Direct the service consumer to the microservice instance endpoint as usual and run the scenario. Happy debugging!

References

Interested in seeing how the IBM Cloud Garage can help you? Sign up for a cost-free, four-hour virtual visit here.

Was this article helpful?
YesNo

More from Cloud

Attention new clients: exciting financial incentives for VMware Cloud Foundation on IBM Cloud

4 min read - New client specials: Get up to 50% off when you commit to a 1- or 3-year term contract on new VCF-as-a-Service offerings, plus an additional value of up to USD 200K in credits through 30 June 2025 when you migrate your VMware workloads to IBM Cloud®.1 Low starting prices: On-demand VCF-as-a-Service deployments begin under USD 200 per month.2 The IBM Cloud benefit: See the potential for a 201%3 return on investment (ROI) over 3 years with reduced downtime, cost and…

24 IBM offerings winning TrustRadius 2024 Top Rated Awards

2 min read - TrustRadius is a buyer intelligence platform for business technology. Comprehensive product information, in-depth customer insights and peer conversations enable buyers to make confident decisions. “Earning a Top Rated Award means the vendor has excellent customer satisfaction and proven credibility. It’s based entirely on reviews and customer sentiment,” said Becky Susko, TrustRadius, Marketing Program Manager of Awards. Top Rated Awards have to be earned: Gain 10+ new reviews in the past 12 months Earn a trScore of 7.5 or higher from…

IBM Tech Now: April 8, 2024

< 1 min read - ​Welcome IBM Tech Now, our video web series featuring the latest and greatest news and announcements in the world of technology. Make sure you subscribe to our YouTube channel to be notified every time a new IBM Tech Now video is published. IBM Tech Now: Episode 96 On this episode, we're covering the following topics: IBM Cloud Logs A collaboration with IBM watsonx.ai and Anaconda IBM offerings in the G2 Spring Reports Stay plugged in You can check out the…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters