Quick revision
The short answer
With the spring-boot-docker-compose module on the classpath, Spring Boot looks for a compose.yaml file when the application starts, runs docker compose up to start the services in it, and waits until they are ready. For services it recognises from their images, such as PostgreSQL or Redis, it creates service connections automatically, so we need no spring.datasource.url or spring.data.redis settings. The lifecycle is configurable with spring.docker.compose.lifecycle-management, the support is skipped in tests by default, and it arrived in Spring Boot 3.1.
What to remember
In this answer, we saw that Docker Compose support, added in Spring Boot 3.1, finds compose.yaml at start-up, runs docker compose up and waits for the services to be ready. For recognised images it creates service connections, ConnectionDetails beans built from the container's port and environment, so no connection properties are needed. spring.docker.compose.lifecycle-management decides whether containers start and stop with the application, the feature is skipped in tests by default, and as a development-only dependency it never reaches production.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With the spring-boot-docker-compose module on the classpath, Spring Boot looks for a compose.yaml file when the application starts, runs docker compose up to start the services in it, and waits until they are ready. For services it recognises from their images, such as PostgreSQL or Redis, it creates service connections automatically, so we need no spring.datasource.url or spring.data.redis settings. The lifecycle is configurable with spring.docker.compose.lifecycle-management, the support is skipped in tests by default, and it arrived in Spring Boot 3.1.
Why this question is asked
Before this feature, running a service locally meant two steps: start the containers by hand, then copy their ports and passwords into application properties, and keep the two in sync. Docker Compose support makes that one step. Interviewers ask about it to see whether we keep up with what Boot now does for us, and whether we understand service connections, which are also the basis of Testcontainers support. In this answer, we will recap Docker Compose, see what Boot does at start-up, look at how service connections replace connection properties, and then cover lifecycle settings and how the feature behaves in tests.
1A Quick Recap of Docker Compose
Docker Compose is a tool that starts several containers from one file. The file, usually called compose.yaml, lists services: each one has an image, ports, environment variables and maybe a health check. The command docker compose up starts them all, and docker compose down stops and removes them.
For a Spring Boot service, this file typically describes the infrastructure the application needs locally: a database, a cache, a message broker. The application itself still runs from the IDE.
Notice that the ports are published without a fixed host port. Docker picks a free port, and Boot reads which one it chose, so two projects can run side by side without clashing.
2What Spring Boot Does at Start-up
The feature lives in the spring-boot-docker-compose module, which we add as a development-only dependency. When the application starts, Boot looks in the working directory for compose.yaml, compose.yml, docker-compose.yaml or docker-compose.yml; spring.docker.compose.file can point elsewhere. It then runs the docker compose command line, so Docker and Compose must be installed.
If the services are not already running, Boot calls docker compose up. It then waits for each service to be ready, using the container's health check if it has one, or by checking that its ports accept connections. Only then does it continue starting the application context, so the DataSource never tries to connect to a database that is still initialising.
6 messages in order. Play them, or step through them one at a time.
Boot starts the containers and wires the application to them before any bean connects.
3Service Connections Replace Connection Properties
A service connection is Boot's way of describing how to reach a service, such as a host, port, username and password, as a ConnectionDetails bean instead of properties. Auto-configuration for the DataSource, Redis, MongoDB, Kafka, RabbitMQ and others reads ConnectionDetails first, and falls back to properties when none exist.
With Docker Compose support, Boot inspects each running container's image name. If it recognises the image, for example postgres, redis, mongo or rabbitmq, it creates the matching connection details from the container's mapped port and environment variables. So for the file above we write no connection properties at all.
The reason this matters is that the random host port changes every time the containers are recreated. With properties, we would have to update them; with service connections, Boot reads the current port at every start.
4Controlling the Lifecycle
By default Boot starts the services when the application starts and stops them when it shuts down. The property spring.docker.compose.lifecycle-management changes that: start-and-stop is the default, start-only leaves the containers running after the application exits, and none means Boot only connects to services we started ourselves.
start-only is a popular choice with DevTools, because it keeps the database and its data alive across many restarts and avoids paying the container start time each time. We can also choose whether stopping means docker compose stop or down, and activate Compose profiles to start only some services.
| Value | On app start | On app stop |
|---|---|---|
| start-and-stop (default) | start services if needed | stop them |
| start-only | start services if needed | leave them running |
| none | do nothing, just connect | do nothing |
5Behaviour in Tests and in Production
Docker Compose support is skipped in tests by default, controlled by spring.docker.compose.skip.in-tests, which is true. Tests are expected to manage their own dependencies, usually with Testcontainers, which starts isolated containers per test run and also creates service connections through @ServiceConnection.
In production, the module should simply not be there. Declared as optional in Maven or developmentOnly in Gradle, it stays out of the packaged jar, so the deployed application reads its real connection properties as usual.
6Follow-up questions
Which Spring Boot version introduced Docker Compose support?
Spring Boot 3.1, together with the ConnectionDetails abstraction and Testcontainers service connections at development time. Later versions added more recognised images and options, so check the reference documentation for your version to see the current list.
What happens if Docker is not running?
The application fails at start-up with an error from the docker compose command, because Boot cannot start or inspect the services. Developers can disable the feature for a run with spring.docker.compose.enabled=false and supply properties themselves.
Can we still override a connection setting, for example the database name?
Yes. For PostgreSQL, Boot reads the database name, user and password from the container's environment variables such as POSTGRES_DB, so changing them in compose.yaml changes the connection. If we need something unusual, we can stop Boot from creating the connection with the ignore label and configure properties as normal.
7Conclusion
In this answer, we saw that Docker Compose support, added in Spring Boot 3.1, finds compose.yaml at start-up, runs docker compose up and waits for the services to be ready. For recognised images it creates service connections, ConnectionDetails beans built from the container's port and environment, so no connection properties are needed. spring.docker.compose.lifecycle-management decides whether containers start and stop with the application, the feature is skipped in tests by default, and as a development-only dependency it never reaches production.
Open with
Spring Boot's Docker Compose support, added in 3.1, makes local infrastructure part of starting the app. I add spring-boot-docker-compose as a development-only dependency and keep a compose.yaml in the project with, say, Postgres and Redis.
Then, if they want more
When I run the application, Boot finds the file, runs docker compose up if the services are not already running, and waits until they are healthy or their ports accept connections.
Then it creates service connections: it looks at each container's image, and for ones it knows, like postgres, redis, mongo or rabbitmq, it builds ConnectionDetails beans from the mapped port and the container's environment variables. Auto-configuration uses those before properties, so I need no datasource URL or Redis host, and random host ports are fine.
For custom images there is a label to map them, and another to ignore a container. The lifecycle is set with spring.docker.compose.lifecycle-management: start-and-stop by default, start-only to keep containers and data across DevTools restarts, or none to just connect.
It is skipped in tests by default, since tests should use Testcontainers, and because it is development-only it is never in the production jar.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.1
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does Spring Boot's Docker Compose support do when you run the application locally?": https://scrivio-interview-library.netlify.app/spring-boot/developer-experience/4ed4e2a886/