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

How fintechs are helping banks accelerate innovation while navigating global regulations

4 min read - Financial institutions are partnering with technology firms—from cloud providers to fintechs—to adopt innovations that help them stay competitive, remain agile and improve the customer experience. However, the biggest hurdle to adopting new technologies is security and regulatory compliance. While third and fourth parties have the potential to introduce risk, they can also be the solution. As enterprises undergo their modernization journeys, fintechs are redefining digital transformation in ways that have never been seen before. This includes using hybrid cloud and…

IBM Cloud expands its VPC operations in Dallas, Texas

3 min read - Everything is bigger in Texas—including the IBM Cloud® Network footprint. Today, IBM Cloud opened its 10th data center in Dallas, Texas, in support of their virtual private cloud (VPC) operations. DAL14, the new addition, is the fourth availability zone in the IBM Cloud area of Dallas, Texas. It complements the existing setup, which includes two network points of presence (PoPs), one federal data center, and one single-zone region (SZR). The facility is designed to help customers use technology such as…

Apache Kafka use cases: Driving innovation across diverse industries

6 min read - Apache Kafka is an open-source, distributed streaming platform that allows developers to build real-time, event-driven applications. With Apache Kafka, developers can build applications that continuously use streaming data records and deliver real-time experiences to users. Whether checking an account balance, streaming Netflix or browsing LinkedIn, today’s users expect near real-time experiences from apps. Apache Kafka’s event-driven architecture was designed to store data and broadcast events in real-time, making it both a message broker and a storage unit that enables real-time…

IBM Newsletters

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