Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Spring Boot with Docker Compose: Selecting a Specific Database Service from Multiple Services

I'm currently facing a challenge with Docker Compose and Spring Boot Docker support. In my compose.yaml file, I've specified two database services, but my intention is to use only one of them, not both. The compose.yaml file is structured as follows:

version: "3"

services:

  server:
    platform: linux/amd64
    image: mysql:8.0.23
    ---

  client:
    platform: linux/amd64
    image: mysql:8.0.23
    ---

The reason for having two services is that I plan to utilize the second service for another application. However, in the current setup, when dealing with a single database service, Spring Boot usually configures the connection details automatically. With two services, I'm encountering difficulty in specifying which one to use. The error message I'm receiving is as follows:

***************************

APPLICATION FAILED TO START

***************************

Description:

Parameter 1 of method dataSource in org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari required a single bean, but 2 were found:

    - jdbcConnectionDetailsForClient_database: defined in unknown location

    - jdbcConnectionDetailsForServer_database: defined in unknown location

This may be due to missing parameter name information

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

How can I configure my setup to use only one database service and not both? Please note that providing information for only one datasource is not resolving the issue.

like image 265
KCS Avatar asked Mar 02 '26 05:03

KCS


1 Answers

I managed to resolve the issue by leveraging Docker's profiles with Compose feature. compose.yaml file is updated as follows:

version: "3"

services:

  server:
    profiles:
      - server
    platform: linux/amd64
    image: mysql:8.0.23
    ---

  client:
    profiles:
      - client
    platform: linux/amd64
    image: mysql:8.0.23
    ---

Additionally, in Spring Boot application.properties file, included the following property:

spring.docker.compose.profiles.active=server

With this configuration, only the services marked with the server profile in Docker compose file will be utilized by the Spring Boot application.

like image 61
KCS Avatar answered Mar 04 '26 17:03

KCS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!