IntelliJ IDEA 2022.3 Help

Run and debug a Spring Boot application using Docker Compose

You can use IntelliJ IDEA to run and debug a Spring Boot application running in multiple Docker containers under Docker Compose. This tutorial describes how to run two Docker Compose services inside containers: a simple Spring Boot application and a MySQL database. The application can receive GET requests that add entries to the database. This tutorial also describes how you can set breakpoints and debug your application using a remote debug configuration.

For features related to Spring, see Enable Spring support in IntelliJ IDEA.

Clone the sample project

The source code of the application is hosted on GitHub at https://github.com/IdeaUJetBrains/SpringBootDockerDemoDebug

  1. From the main menu, select File | New | Project from Version Control.

  2. Specify the URL of the repository and click Clone.

  3. If necessary, agree to open the cloned project in a new window.

Clone sample Spring Boot project for Docker

Run the application using Docker Compose

Let's see how the application works before running it in debug mode.

  1. Open the docker-compose-debug.yml file.

  2. Click docker-compose up in the gutter next to services.

This creates a Docker Compose run configuration, which starts the application in a container as the app service and the database in another container as the db service. If successful, you should see something similar to the following in the build log:

⠿ Container db Healthy 10.6s ⠿ Container springbootdockerdemodebug-app-1 Started 10.9s

Running Spring Boot application in Services tool window

You can access the application at http://localhost:18080. For example, you can try to execute the following GET request, which should add a new entitybus entry to the database and list all available entries: http://localhost:18080/entitybus/post.

Execute a GET request

You can connect to the database at jdbc:mysql://0.0.0.0:13306/DOCKERDB. For example, you can use the Database tool window to add the database as a data source and check the entitybus table. Use the following connection settings:

Host

0.0.0.0 or localhost or 127.0.0.1

Port

13306

User

root

Password

root

Database

DOCKERDB

Connect to MySQL

Create a remote debug configuration

To debug the application, you need a remote debug configuration that will first run the application in Docker Compose with a custom command for debugging, and then attach to the debugger.

  1. Open the docker-compose-debug.yml file.

  2. Click Debug with Remote in the gutter next to command under the app service.

  3. In the Configure Remote JVM Debug dialog, change the host to 0.0.0.0 to make it listen on all interfaces, then select the demo module in the Use module classpath list.

  4. Double-click the Docker Compose run configuration in the Before launch list. If it is not in the list, click the Add button and select Launch Docker before debug.

  5. Select the Docker Compose run configuration with the app service. Also check the Custom Command field: it should contain the -agentlib option and options from the command field in the docker-compose-debug.yml file:

    java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 -Djava.security.egd=file:/dev/./urandom -jar /project/target/demo-0.0.1-SNAPSHOT.jar
Configure remote JVM debug for Docker Compose

If the application is already running from the previous step, apply the changes and do not run the debug configuration for now.

Launch the debug configuration

  1. If the application is already running, stop it. To do it, select the Docker Compose node in the Services tool window and click The Down button in the toolbar. Alternatively, you can right-click and delete the containers under the corresponding services.

  2. Open the docker-compose-debug.yml file.

  3. Click Debug with Remote in the gutter and start the debug configuration.

Once the application starts and the debugger attaches to it, the Debug tool window will open.

The Debug tool window

Set a breakpoint and debug the application

  1. Open the src/main/java/entity/Entitybus.java file and set a breakpoint in the setEid() method.

    Breakpoint in the setEid() method
  2. Execute the following GET request: http://localhost:18080/entitybus/post.

    The application will stop at the breakpoint and you can examine the current variable values and frames in the Debug tool window.

    Stopped at breakpoint
  3. Click the Resume Program button until it executes the request and you get the returned values.

Last modified: 27 December 2022