Computer scienceBackendSpring BootIntroduction to Spring Boot

Docker Compose support in Spring Boot

Most real applications don't run alone. They need a database, a message broker, or a cache, and every developer has to get those running locally before the first request can be served. You can install each service by hand, or you can describe them once in a Docker Compose file and let Spring Boot start them for you.

In this topic, you'll learn how to add Docker Compose support to a Spring Boot application, how Spring Boot connects to the containers without any connection settings, and how to control when they start and stop.

The problem it solves

Suppose your application stores data in PostgreSQL. You already know that Spring Boot needs the database's URL, username, and password:

spring.datasource.url=jdbc:postgresql://localhost:5432/shop
spring.datasource.username=app
spring.datasource.password=secret

You also have a compose.yaml file that describes the database container, so you can start it with docker compose up:

services:
  postgres:
    image: 'postgres:17'
    environment:
      - 'POSTGRES_DB=shop'
      - 'POSTGRES_USER=app'
      - 'POSTGRES_PASSWORD=secret'
    ports:
      - '5432'

Now the same information lives in two places, and you have to remember to start the container before the application. If someone changes the password in one file, the other becomes wrong. Spring Boot's Docker Compose support removes both problems: it starts the services from the file and reads the connection details from the running containers.

Adding the support

You need two things. First, Docker with Compose must be installed, and the docker and docker compose commands must be available on your path. The minimum supported Compose version is 2.2.0. Second, add the spring-boot-docker-compose module to your project:

// build.gradle.kts
dependencies {
    developmentOnly("org.springframework.boot:spring-boot-docker-compose")
}

In Maven, the same dependency is marked <optional>true</optional>. Both mean that the module is available when you run the application from your IDE or build tool, but it is not packaged into the final executable archive by default. This is intentional: Compose support is a development convenience, and a production environment should get its services in some other way.

Put the compose.yaml file in the working directory, which is normally the root of your project. Spring Boot looks for compose.yaml, compose.yml, docker-compose.yaml, and docker-compose.yml, in this order, and uses the first one it finds. If your file has another name or location, set the spring.docker.compose.file property.

What happens at startup

With the module on the classpath, running your application does the following:

  1. Spring Boot looks for a Compose file in the working directory.

  2. It calls docker compose up with that file.

  3. It creates service connection beans for each container it recognizes.

  4. When the application shuts down, it calls docker compose stop.

If the services are already running when the application starts, Spring Boot skips the up step, and it will not stop them at shutdown either. It only creates the connection beans. This means you can start the containers yourself and still benefit from the automatic configuration.

Service connections

A service connection is Spring Boot's way of describing how to reach a service. Auto-configuration, such as the one for the data source, uses it instead of the connection properties. This is why the spring.datasource.* lines from above are no longer needed: you can delete them, and the application still connects.

Spring Boot recognizes a container by its image name. An image called postgres is treated as PostgreSQL, mongo as MongoDB, redis as Redis, rabbitmq as RabbitMQ, and so on. The documentation lists many more, including MySQL, MariaDB, Elasticsearch, and Neo4j. For each one, Spring Boot reads the connection information from the container, such as the host, the port, and the credentials that you gave through its environment variables.

One detail is easy to miss. The application connects to the port mapped on your computer, not to the container's own port. Notice that the Compose file above says '5432' and not '5432:5432'. When you give only the container port, Docker chooses a free port on your machine, so the container never conflicts with a database that is already running locally, and Spring Boot finds the chosen port for you.

If your image has a different name, for example your company's own build of Redis, tell Spring Boot what kind of service it is with a label. This only works if the image behaves like the standard one, including the environment variables it supports:

services:
  cache:
    image: 'mycompany/custom-redis:7.0'
    ports:
      - '6379'
    labels:
      org.springframework.boot.service-connection: redis

If you want Spring Boot to ignore a container completely, label it with org.springframework.boot.ignore: true.

Waiting until a service is ready

A started container is not necessarily a working service. A database needs a few seconds before it accepts connections, and an application that connects too early would fail. Spring Boot waits until each container is ready before it continues.

The best way to define readiness is to add a healthcheck to the service in your Compose file, because only the service's own check knows what "ready" means. Without one, Spring Boot falls back to a simple test: the container is considered ready when a TCP connection to its mapped port succeeds. You can disable that fallback for one container with the label org.springframework.boot.readiness-check.tcp.disable: true, and you can set a limit for the whole wait with spring.docker.compose.readiness.timeout.

Controlling the lifecycle

The spring.docker.compose.lifecycle-management property decides what Spring Boot does with the containers:

Value

Behavior

none

Does not start or stop Compose.

start-only

Starts Compose when the application starts and leaves the containers running.

start-and-stop

Starts Compose when the application starts and stops it when the JVM exits. This is the default.

Choose start-only when restarting a database is slow or when you want its data to be there immediately, for example while you restart the application many times in a row. It is also the right choice when several applications share the same services. In that case, create the Compose file in one application and point the others to it with spring.docker.compose.file. With the default setting, stopping one application would also stop services that the others still need.

You can also change which Compose commands are used. For example, spring.docker.compose.stop.command=down removes the containers when the application stops, instead of only stopping them. With the default, stopped containers keep their data, and the next start is quicker. If you want a completely clean state, down removes the containers, and you can pass --volumes through spring.docker.compose.stop.arguments[0] to remove the volumes too.

Other properties let you activate a Compose profile (spring.docker.compose.profiles.active) or pass additional arguments to the Compose commands.

Docker Compose in tests

By default, Docker Compose support is switched off in tests. The logic is that tests are usually expected to prepare their own environment. If you want your tests to start the services from your Compose file as well, set spring.docker.compose.skip.in-tests=false, and in Gradle change the dependency's configuration from developmentOnly to testAndDevelopmentOnly.

Docker Compose or Testcontainers?

Spring Boot offers another way to start services during development: Testcontainers. The two solve a similar problem in different ways:

Docker Compose

Testcontainers

Where services are described

In a compose.yaml file

In Java or Kotlin code, as beans

How you run it

Start the application normally

Start a special launcher class from your test sources

Connection details

Detected by image name or label

Declared with @ServiceConnection

Tests

Off by default

Designed for use in tests

Compose is a good fit when your team already uses a Compose file, or when the same file is used outside of Spring, for example by non-Java services or in a CI pipeline. Testcontainers is a good fit when you want the services defined in code and written directly into integration tests. Neither is better in general, and a project may well use both.

Practical notes

  • Docker must be running. If it isn't, the application cannot start its services and will fail during startup. Check Docker first when you see an error that mentions the docker command.

  • Keep it for development. The module is not packaged by default. In production, provide the connection properties for the real services through your normal configuration.

  • The Compose file is the source of truth. Service connections are used in place of the connection properties, so an old spring.datasource.url left in your configuration has no effect on a recognized container. To change the credentials or the database name, change the Compose file.

  • Pin your image versions. Use postgres:17 and not postgres:latest, so that everyone on the team and in CI runs the same version.

Conclusion

Spring Boot's Docker Compose support starts the services described in your compose.yaml when the application starts, and creates service connections for the containers it recognizes by image name or by label. You no longer repeat host, port, and credentials in your configuration, and the mapped port is found for you.

You add it with the spring-boot-docker-compose module, which is kept out of the packaged application by default. The lifecycle-management property decides whether the containers are stopped with the application, readiness is best defined with a healthcheck, and tests are excluded unless you opt in.

How did you like the theory?
Report a typo