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.
2
Answers
I managed to resolve the issue by leveraging Docker's profiles with Compose feature.
compose.yaml
file is updated as follows:Additionally, in Spring Boot
application.properties
file, included the following property:With this configuration, only the services marked with the
server
profile in Docker compose file will be utilized by the Spring Boot application.You will need to run it on 2 different ports, Both can’t be available on 3306 (default port)
Also you will need to define url, username and password for both one could be under spring.datasource (url, username, password) other could possibly be spring.customdb (url, username, password)
Create a @Configuration and add @Bean for DataSource bean for second database by reading application.yml/.properties file (spring.customdb params)
Primary datasource can be created by spring at the startup available in (spring.datasource)