IntelliJ IDEA 2024.1 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 in the same virtual network: 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.

For features related to Spring, refer to 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. In the main menu, go to 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

Prepare the Spring Boot run configuration

The project should have the DemoApplication configuration, which runs the Spring Boot application. By default, it runs locally, but you need to run it on the container from the Docker Compose java service.

  1. In the main menu, go to Run | Edit Configurations.

  2. In the Run/Debug Configurations dialog, select the DemoApplication configuration, expand the Run on list, and select Docker Compose under Create New Targets.

    Add new Docker Compose target
  3. Select the docker-compose.yml file, then the java service, and click Next.

    Add Docker Compose run target with Java service
  4. After IntelliJ IDEA builds the target image and detects the Java home path, click Next.

  5. Check the detected Java home path and version and click Create.

  6. Make sure that the new java service run target is selected and apply the changes to the DemoApplication configuration.

    DemoApplication with new run target

You can configure a run target from a Dockerfile or an image, but in this tutorial we are using Docker Compose to run two services in the same network, so that the Java application in one service can access the database in the other service.

Prepare the database run configuration

You need to run the database for the application from the db service defined in the same docker-compose.yml file. Create a run configuration for it and then configure to run it before the Spring Boot application.

  1. In the Run/Debug Configurations dialog, click Add New Configuration the Add New Configuration button and select Docker Compose.

  2. Give this configuration a descriptive name like DatabaseService, specify the docker-compose.yml file, and then add the db service.

    Docker Compose configuration for the database service
  3. Apply the changes to the new configuration and select the DemoApplication configuration.

  4. In the DemoApplication configuration, click Modify options, select Add before launch task, then click Run Another Configuration, and add the new Docker Compose configuration to run the database service before the main application.

    DemoApplication run config with Docker Compose as a Before Launch task
  5. After you apply the changes, run the DemoApplication configuration.

Run the application

When you run the DemoApplication configuration, IntelliJ IDEA will first start the db Docker Compose service and make sure that the database is healthy. Then it will start the java service as a run target for the Spring Boot application. Finally, it will use the Java runtime from the run target for the Spring Boot application. If everything is correct, you should see the following output:

Console output after successfully running the Spring Boot application
  1. Note the random local port number that Docker forwards to port 8080 in the container where the application is running. In the example above, it is 54123.

  2. You can access the application and list the entities stored in the database at http://localhost:54123/entitybus (replace the port number with whatever Docker assigns in your case).

  3. To add a random entity to the database, send a GET request to http://localhost:54123/entitybus/post.

Execute a GET request

Connect to the database

If you want, you can connect to the running database at jdbc:mysql://0.0.0.0:13306/DOCKERDB.

  • Open the Database tool window and add a MySQL data souce with the following settings:

    Host

    localhost

    Port

    13306

    User

    root

    Password

    root

    Database

    DOCKERDB

Connect to MySQL

Debug the application

  1. Run DemoApplication in debug mode. For example, with the run configuration selected, click the Debug button in the main toolbar.

  2. Open the EntitybusController.java file and set a breakpoint somewhere inside the postEntity() method. For example, on line number 27.

  3. Open http://localhost:54123/entitybus/post in your browser (or whatever port Docker assigned in your case).

The debugger should suspend execution at the breakpoint, and you can examine the current state, such as variable values and frames.

Spring Boot application suspended at breakpoint
Last modified: 11 February 2024