Skip to content

Start method is called twice on DockerComposeLifecycleManager during startup #35878

@jvwilge

Description

@jvwilge

The DockerComposeLifecycleManager#start() method is called twice on startup of a spring boot application, resulting in an error:

The bean 'jdbcConnectionDetailsForDemoPostgres' could not be registered. A bean with that name has already been defined and overriding is disabled.

I get it that this is a problem, the cause however I'm a bit in the dark about.

I discovered that the DockerComposeLifecycleManager#start() method is called twice. Both times from DockerComposeListener#onApplicationEvent(ApplicationPreparedEvent event).
Going up the stack the first call originates from SpringApplication#310 (prepareContext) and the second from SpringApplication#311 (refreshContext).

It only happens with spring-cloud-starter-config as a dependency.

Possible casue

Digging on the internet I suspect it has something to do with the applicationContext now being in a hierarchy :

Why Spring Boot Application logs that it started twice after adding spring-cloud-bus dependency

So if this is the case I think the mentioned check (if (event.getApplicationContext().equals(this.applicationContext)) {) should be in DockerComposeListener, but I could be totally wrong and this is something else, I'm not that familiar with cloud config yet.

About ApplicationEvent and BootStrap #1119 and
Best way in custom Spring Boot Starter library to read current properties and set new ones based on values might also be related.

Reproduce

To reproduce create a Maven Spring Boot 3.1.0 with 'Docker Compose Support', 'Config Client' and 'Web' on start.spring.io

Replace compose.yaml with:

version: "3.8"

services:
  postgresql:
    image: 'postgres:15.3-bullseye'
    container_name: 'demo-postgres'
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=demodb
    ports:
      - "5432:5432"
    user: root
    volumes:
      - 'pg_data:/var/lib/postgresql/data'

volumes:
  pg_data:

Add the following to application.properties:

spring.config.import=optional:configserver:

To 'fix' the problem comment the line added to application.properties and comment the dependency spring-cloud-starter-config in pom.xml.

I'm impressed with the feature set of spring-boot-docker-compose by the way, it will make a lot of things easier for us!

Metadata

Metadata

Assignees

Labels

for: external-projectFor an external project and not something we can fix

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions