Light-4j applications are standalone Java applications without any JEE container and can be debugged inside IntelliJ or Eclipse directly. Most of the time, we are going to debug the application before dockerizing it to ensure it is functioning. Sometimes, an application works when starting with java -jar xxx.jar but when running with a docker container, it stops working. Most cases, this is due to the docker network issue.
In case this happens, it would be great if we can debug the application running inside the Docker container remotely from your favorite IDE. In this tutorial, we are going to walk through the steps with IntelliJ IDEA. For developers who are using Eclipse, the process should be very similar.
First, we need to create a Dockerfile with debug agent in the java command line.
Before running the docker-compose, we need to make sure that Consul docker-compose is running.
docker-compose -f docker-compose-consul.yml up -d
Now, the local image for the light-router is the debug image. Let’s take a look at the docker-compose file that started two instances of light-codegen and light-router. This file can be found in the light-config-test along with all configurations.
Before starting the docker-compose, we might need to run docker-compose rm to remove the reference to the old image.
You will notice that the light-router application within the container is not up and running but output the following line.
light-router_1 | Listening for transport dt_socket at address: 5005
This line indicates that the application inside the container is waiting for IDE debug connection on port number 5005. Let’s open the light-router project in IDEA. To set up the remote debug, follow the steps below.
Click Run tab and Debug Configuration menu.
Click + to add a remote application called remote.
Setup the parameters as below
Click Apply and OK to close the popup window.
Click Run tab and Debug menu and select remote in the popup dropdown to start the debug session.
You can set the breakpoints in the initialization code if you want to debug the logic during the server startup. Once the debug session is started, the light-router application inside the container will be started and running as usual. The rest of the debug is the same as you are debugging a standalone application.