Four Steps to Debugging Java Spring Boot Microservices Running in Docker Containers
5 min read
By: Shili Yang
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:
Build the service in debug mode.
Modify the Dockerfile to expose the debug port on the Docker image, and start/run the application in debug mode.
Deploy the Docker container with the debugger port exposed.
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
Modifying the Dockerfile for building the container image
Next, modify the Dockerfile for building the container image of the service to do the following:
Start the service in debug mode
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
-Xdebug:to enable debugging
-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=nto configure the JDWP protocol for remote debugging (the port
8000is 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
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!