-
Notifications
You must be signed in to change notification settings - Fork 40.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document how to add parameters to the JDBC URL created for a Docker Compose managed container #35636
Comments
Assuming that you have an SQL database configured in |
@wilkinsona Thanks. Is there any documentation available about moving property based configurations (like JDBC url) to the docker-compose file so that Spring will pick up those values from the compose file? Thank you! |
It doesn't really make sense to move the JDBC URL to the compose file as they're two competing sources of the information that's required to connect to the database. Boot will honour the environment variables that the container supports. For example, if you configure Lines 39 to 43 in 6a74f63
Line 60 in 6a74f63
Can you describe what you're trying to do? |
Sure - We have a postgres database that we want to start via docker-compose when starting our app for local development. Inside the docker image, we have a postgres database that is using an application specific database name "platform_db" with a specific schema name "application". Previously when we used the properties to configure the JDBC url, we could provide these values to Spring on startup so that when the connection pool was created, it defaulted to the custom postgres database and schema. With docker-compose support enabled, Spring ignores our configured database and schema names and tries to connect to our docker postgres instance using the "postgres" database and default schema - which is a default setting. So we are then unable to connect to our database in our application when using the docker-compose addon. Based on your linked code, it looks like we might be able to specify database name but not schema. Thanks! |
Yes, I think that's the case. While not very elegant, you can make it work by post-processing the @Bean
static BeanPostProcessor jdbcConnectionDetailsPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (!(bean instanceof JdbcConnectionDetails)) {
return bean;
}
JdbcConnectionDetails jdbc = (JdbcConnectionDetails) bean;
return new JdbcConnectionDetails() {
@Override
public String getUsername() {
return jdbc.getUsername();
}
@Override
public String getPassword() {
return jdbc.getPassword();
}
@Override
public String getJdbcUrl() {
return jdbc.getJdbcUrl() + "?currentSchema=application";
}
};
}
};
} |
OK thanks. The reason we added the docker-compose addons was for convenience but I am also not a fan of adding in unnecessary code to the codebase when we had something working in our configuration file. Not sure if this could be turned into an enhancement request? |
Yes, I think so. There definitely appears to be some room for improvement here. In the meantime, you may want to label your Postgres container with |
Thank you - I will try to ignore option. I think this will work for us. |
Adding a label to the service
should work, too. We should maybe document that. |
Thanks, @mhalbritter. I'd forgotten we had that 😬. I think this is just a documentation issue now. |
Thank you! Adding
did the trick along with the environment variable: |
Version: Spring Boot 3.1
application.yml:
If I do not include:
then when I start the app, Spring uses the correct JDBC url. Note I have providing a specific database and schema.
When I include docker-compose support, HikariCP ignores my configuration:
and the application fails to run because it's ignoring my JDBC url.
Thanks
The text was updated successfully, but these errors were encountered: