14.7.2023

Spring boot docker compose Support (since 3.1.1)

Starting from Spring Boot version 3.1.1, developers can leverage the enhanced Docker Compose support, which brings greater convenience and flexibility to the deployment process of Spring Boot applications.

The new Docker Compose support in Spring Boot 3.1.1 allows developers to define multi-container environments with ease, enabling them to specify the services, networks, volumes, and their interconnections using a single docker-compose.yml file.

With this native support, managing complex containerized deployments becomes more straightforward, as developers can now define the composition of their Spring Boot application and its dependencies within the familiar context of a Docker Compose file.

Additionally, Spring Boot's Docker Compose support includes built-in integration with container orchestration platforms like Kubernetes, enabling seamless deployment of Spring Boot applications in orchestrated environments.

By integrating Docker Compose into Spring Boot, version 3.1.1 empowers developers to adopt containerization practices more effectively, facilitating smoother development-to-production workflows and enhancing the scalability and resilience of Spring Boot applications.

How?

Add the new Spring Boot package to your build.gradle:

...
implementation 'org.springframework.boot:spring-boot-docker-compose'
...

Put the docker-compose.yml somewhere in the project (e.g. /docker-compose.yml, main/resources/docker-compose.yml)

Link the docker compose file in your applications.properties or application.yaml

spring:
  docker:
    compose:
      file: "./docker-compose.yml"

The docker-compose.yaml looks like this and is just a regular docker-compose file

version: "3.8"
services:
  postgres:
    image: postgis/postgis:13-master
    ports:
      - '5432:5432'
    environment:
      POSTGIS_GDAL_ENABLED_DRIVERS: ENABLE_ALL
      POSTGRES_DB: geo-postgres
      POSTGRES_USER: username
      POSTGRES_PASSWORD: mysecretpassword
      POSTGRES_HOST: localhost
      POSTGRES_PORT: 5432

Spring Boot will then start the Containers listed in the compose file on Start e.g. bootRun if they are not already started. On System exit of the application, Spring Boot will stop the Containers without the need to manually interfere in the process.

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.1)

2023-07-06T12:40:53.844+02:00  INFO 73300 --- [           main] c.n.geosample.GeoSampleApplication       : Starting GeoSampleApplication using Java 18.0.2.1 with PID 73300 (/Users/simonjakubowski/code/geo-sample/build/classes/java/main started by simonjakubowski in /Users/simonjakubowski/code/geo-sample)
2023-07-06T12:40:53.850+02:00  INFO 73300 --- [           main] c.n.geosample.GeoSampleApplication       : No active profile set, falling back to 1 default profile: "default"
2023-07-06T12:40:53.997+02:00  INFO 73300 --- [           main] .s.b.d.c.l.DockerComposeLifecycleManager : Using Docker Compose file '/Users/simonjakubowski/code/geo-sample/docker-compose.yml'
2023-07-06T12:40:54.824+02:00  INFO 73300 --- [utReader-stderr] o.s.boot.docker.compose.core.DockerCli   : Container geo-sample-postgres-1  Created
2023-07-06T12:40:54.828+02:00  INFO 73300 --- [utReader-stderr] o.s.boot.docker.compose.core.DockerCli   : Container geo-sample-postgres-1  Starting
2023-07-06T12:40:55.120+02:00  INFO 73300 --- [utReader-stderr] o.s.boot.docker.compose.core.DockerCli   : Container geo-sample-postgres-1  Started
2023-07-06T12:40:55.124+02:00  INFO 73300 --- [utReader-stderr] o.s.boot.docker.compose.core.DockerCli   : Container geo-sample-postgres-1  Waiting
2023-07-06T12:40:55.640+02:00  INFO 73300 --- [utReader-stderr] o.s.boot.docker.compose.core.DockerCli   : Container geo-sample-postgres-1  Healthy
2023-07-06T12:40:56.818+02:00  INFO 73300 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-07-06T12:40:56.868+02:00  INFO 73300 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 45 ms. Found 1 JPA repository interfaces.
2023-07-06T12:40:57.378+02:00  INFO 73300 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)

By default, Spring will not start the docker containers when executing tests. You can enable this by turning the following option

spring:
  docker:
    compose:
      file: "./docker-compose.yml"
      skip:
        in-tests: false
Simon

Softwareentwickler

Zur Übersicht

Standort Hannover

newcubator GmbH
Bödekerstraße 22
30161 Hannover

Standort Dortmund

newcubator GmbH
Westenhellweg 85-89
44137 Dortmund