spring-boot: Unable to use Docker Compose support when mixing dedicated and shared services

I have a microservices-based mono-repo with multiple spring-boot applications. I would like to use Spring Boot Docker Compose support to start the dependent services. There are few services (ex: db) that are specific to each service and there are some services (ex: RabbitMQ) shared by multiple services.

All the microservices dependencies are defined in a single compose.yml fil. If I start any one microservice it starts all the services defined in the compose.yml file, which is ok for me.

version: "3.8"
services:
  catalog-db:
    image: postgres:16-alpine
    ...
  orders-db:
    image: postgres:16-alpine
    ...
  rabbitmq:
    image: rabbitmq:3.12.11-management
    ...

But the problem is, when I have multiple databases defined for different services, Flyway initialization fails.

Parameter 0 of method flyway in org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration required a single bean, but 2 were found:
	- flywayConnectionDetailsForCatalogDb: defined in unknown location
	- flywayConnectionDetailsForOrdersDb: defined in unknown location

Is there a way to configure at the microservice level (using spring.docker.compose.* properties) to take only certain services into account?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

--orphans=false did the trick. I’ve also added an integration test which tests that this indeed works.

What could work is:

Run docker compose --profile <profile> config --format=json. This returns the config for the services filtered by profile in JSON format:

{
  "name": "sb-40139",
  "networks": {
    "default": {
      "name": "sb-40139_default",
      "ipam": {}
    }
  },
  "services": {
    "order-db": {
      "profiles": [
        "order"
      ],
      "command": null,
      "entrypoint": null,
      "environment": {
        "POSTGRES_DB": "mydatabase",
        "POSTGRES_PASSWORD": "secret",
        "POSTGRES_USER": "myuser"
      },
      "image": "postgres:17",
      "networks": {
        "default": null
      },
      "ports": [
        {
          "mode": "ingress",
          "target": 5432,
          "protocol": "tcp"
        }
      ]
    },
    "redis": {
      "command": null,
      "entrypoint": null,
      "image": "redis:7",
      "networks": {
        "default": null
      },
      "ports": [
        {
          "mode": "ingress",
          "target": 6379,
          "protocol": "tcp"
        }
      ]
    }
  }
}

Then run docker compose ps --format=json, which returns all running containers for the project (doesn’t do profile filtering):

// ...
{
  "Command": "\"docker-entrypoint.s…\"",
  "CreatedAt": "2024-12-09 13:53:46 +0100 CET",
  "ExitCode": 0,
  "Health": "",
  "ID": "eae6fa2df78e",
  "Image": "postgres:17",
  "Labels": "...",
  "LocalVolumes": "1",
  "Mounts": "577790079dbe43…",
  "Name": "sb-40139-order-db-1",
  "Names": "sb-40139-order-db-1",
  "Networks": "sb-40139_default",
  "Ports": "0.0.0.0:32812->5432/tcp, :::32812->5432/tcp",
  "Project": "sb-40139",
  "Publishers": [
    {
      "URL": "0.0.0.0",
      "TargetPort": 5432,
      "PublishedPort": 32812,
      "Protocol": "tcp"
    },
    {
      "URL": "::",
      "TargetPort": 5432,
      "PublishedPort": 32812,
      "Protocol": "tcp"
    }
  ],
  "RunningFor": "8 minutes ago",
  "Service": "order-db",
  "Size": "0B",
  "State": "running",
  "Status": "Up 8 minutes"
}
// ...

Now we can look in the config from above (under the services object) using the docker compose ps Service field. If it’s not in the config, we totally ignore the running container.

This way, when specifying e.g. spring.docker.compose.profiles.active=order it should only return running containers belonging to the order profile.

@sach429 this looks like supporting profiles in docker ps through custom labels. I think https://github.com/docker/compose/issues/12361 is much more promis in because this means support for the entire docker compose ecosystem.

I tried to split them into separate files.

Another option would be to keep the services in one file, use Docker Compose profiles, and modify your application.properties to control when individual services are started.

I see the following message due to which the required services are not being started.

This behavior is mentioned in the documentation:

If the Docker Compose services are already running when starting the application, Spring Boot will only create the service connection beans for each supported container. It will not call docker compose up again and it will not call docker compose stop when the application is shutdown.

There is some discussion on why we chose to implement things this way in https://github.com/spring-projects/spring-boot/issues/38398.

You can set spring.docker.compose.lifecycle-management=none to prevent this message. You will have to start the service manually (for example, with docker compose up), but Spring Boot will create the service connections as expected.

An additional mode like the one suggested in #39749, where docker compose up would be called without first checking to see what services are running, could help this situation. I will close this issue in favor of that issue.