Spring Boot with docker compose


Hello Everyone, Today we are going to talk one of the most important features in latest version of spring boot 3 and spring 6. Spring Boot has introduced inbuilt support for docker compose via a module called spring-boot-docker-compose.

We will first talk about what docker compose is and then we will start diving into spring boot support for the same.

Docker Compose:

As per the official docker compose documentation, "Docker Compose is a tool for defining and running multi-container Docker applications.

Generally when you are building any kind of software, you interact with lot of different applications. for e.g if you are building a e-commerce web application using spring-boot or any popular java framework, you will need front-end application code, backend application with application server, an database, probably a caching service like redis/EHcache. Without docker compose for each one of these applications configuration you will have to define Dockerfile and then build those docker images and container.

So, In a nutshell, If you have more than one Dockerfile use docker compose. It provides a single configuration file for all the services (i.e one service per applications like your backend app, database) and with a single command you can create docker containers for these services and wire them up together. 

Sample Docker compose file:

  version: '3.1'
      build: backend
        - "7790:8080"
        - spring-mysql
      image: mysql
      restart: always
        - "7791:3306"
        MYSQL_USER: root
        MYSQL_PASSWORD: password
        MYSQL_DATABASE: blogs
        - spring-mysql


How to run docker file:

 docker compose up  

How does Spring Boot 3 helps here

With the new module called spring-boot-docker-compose, you don't need to execute above command to create the containers. 

May be some of you will say what is the use of this single line work? 

But with this new support if you have the spring-boot-docker-compose dependency in class-path, spring boot will automatically take care of creating all the required docker containers for you and its easy in automated deployment pipelines.

Dependency to add in your maven configuration of spring boot project.


How does spring boot finds out the docker compose:

Spring boot will look for the below files in class-path:
  1. compose.yaml or  compose.yml
  2. docker-compose.yaml or docker-compose.yml
if you want to refer custom file name add below property in application.properties.

spring.docker.compose.file = custom.yaml

where custom.yaml is your file name.

Post a Comment


Please Select Embedded Mode To show the Comment System.*

Cookies Consent

This website uses cookies to offer you a better Browsing Experience. By using our website, You agree to the use of Cookies

Privacy Policy