A docker-compose.yml file describes every container of a multi-container application, such as a Spring Boot service, its database and a cache, in one declarative file. You start, stop and rebuild the whole stack with a single docker compose command. Since Spring Boot 3.1 the application can even start and connect to those containers by itself during development.
The current name recommended by the Compose Specification is compose.yaml, but docker-compose.yml is still read everywhere, so existing projects keep working.
A Compose file lists the pieces of your application and how they connect:
services: the containers to run, each with an image or a build context.networks: how containers reach each other. Compose creates a default network, so services find each other by service name.volumes: named storage that survives container restarts.environment, ports, healthcheck and depends_on: runtime settings for each service.Because the file lives in your repository, every developer and CI job starts the same stack with the same versions.
Key points in this file:
db, which is the service name, not localhost.depends_on with condition: service_healthy makes Compose wait until PostgreSQL passes its health check. Plain depends_on only waits for the container to start, not for the database to accept connections.pgdata volume keeps the data when you recreate the container.version: key. The Compose Specification made it obsolete, and current Docker Compose ignores it with a warning.Use the docker compose plugin (Compose v2). The old standalone docker-compose binary (v1) is no longer maintained.
Spring Boot 3.1 added the spring-boot-docker-compose module. With it on the classpath, running the application starts the services from your Compose file, and Boot configures the connection details automatically, so you do not need to hard-code the datasource URL.
For a Compose file that contains only the database, the application then starts it, detects the PostgreSQL service and wires spring.datasource.* for you.
Useful properties include spring.docker.compose.file, spring.docker.compose.lifecycle-management (none, start-only, start-and-stop) and spring.docker.compose.skip.in-tests. This support is meant for local development, so keep it out of production images.
.env file that is not committed, env_file, or Compose secrets.postgres:17 instead of latest so builds are reproducible.service_healthy for ordering.--profile tools.compose.override.yaml or multiple -f files, one for development and one for CI.Compose runs containers on a single Docker host. It is excellent for local development, integration tests and small deployments. For multi-node scheduling, rolling updates and autoscaling, teams use Kubernetes or a managed container service, and they can still reuse the same images that Compose builds.
docker-compose.yml or compose.yaml?Both work. Docker Compose looks for compose.yaml first and also accepts compose.yml, docker-compose.yaml and docker-compose.yml. New projects should prefer compose.yaml.
version field?No. It is obsolete in the Compose Specification and only triggers a warning in current Compose releases.
localhost:5432 inside Compose?Inside a container, localhost is the container itself. Use the service name, for example db, as the host name.
depends_on wait for the database to be ready?Only with a health check and condition: service_healthy. Otherwise it waits only for the container to start.
docker-compose.yml captures your whole application stack as code. Define services, networks, volumes and health checks in it, run it with docker compose, rely on service names for networking, and on Spring Boot 3.1 or later use spring-boot-docker-compose to start and connect to the services automatically in development.