Docker Compose support in Spring Boot applications

We often use Docker Compose to run multiple applications with the database or with other services that the application is using, at the same time, using multiple Docker containers. From version 3.1 onwards, Spring Boot also supports Docker Compose, making it much easier to develop Spring Boot applications. How is it in detail? Let’s find out together in this tutorial!

First, I will create a new Spring Boot project with Spring Data JPA, PostgreSQL Driver and Docker Compose Support:

for example.

As you can see, when I create a Spring Boot project with Docker Compose Support, a compose.yaml file is also created.

Check this compose.yaml file, you will see a postgres service has been declared, as follows:

That’s because I declared to use the PostgreSQL Driver!

You need to edit the ports section a bit to expose port 5432 of the PostgreSQL database to the outside, as follows:

Now, if you run this Spring Boot application, you will see Spring Boot automatically pull the Docker Image of PostgreSQL, the result is as follows:

Since my project currently has nothing, after pulling the Docker Image of PostgreSQL, my application will automatically shut down.

To make an example for this project, I will connect to the PostgreSQL database to get student information.

I will define the student table as follows:

To keep it simple, I will save the DDL of the student table to the db.sql file in the src/main/resources folder of the project and edit the postgres service in the compose.yaml file so that it automatically initializes the student table when running Docker Container for me, as follows:

Now, I will declare the database information for my application in the application.properties file as follows:

I will also declare the Student entity and the Repository class for this entity as follows:

and:

Run the application with Spring Boot’s CommandLineRunner:

You will see the following results:

As you can see, my application has connected to the PostgreSQL Docker Container that Spring Boot launched when I ran the Spring Boot application.

Another support of Spring Boot for Docker Compose is that we do not need to declare PostgreSQL information in the application.properties file!

Information about the connection to the PostgreSQL database will be contained in the bean of the ConnectionDetails class.

You can delete the database configuration content that we declared above in the application.properties file and run the application again. You will also see the same result.

Try injecting the bean of the ConnectionDetails class and printing the information of the PostgreSQL database:

You will see the following results:

If you do not expose the PostgreSQL port to the outside but only declare it as follows in the compose.yaml file:

PostgreSQL database will be exposed to the outside using random port and our application can also connect to this port. For my example, if you run it again, you will see the same result as me as follows:

This is very useful if you are developing microservices applications, each service uses a separate database. Random port will help us avoid port conflicts!

Add Comment