Skip to content
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

Closed
GrantGochnauer opened this issue May 25, 2023 · 11 comments
Assignees
Labels
type: documentation A documentation update
Milestone

Comments

@GrantGochnauer
Copy link

Version: Spring Boot 3.1

application.yml:

spring:
  docker:
    compose:
      file: "./docker-compose.yml"
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:15432/platform_db?currentSchema=application
    username: postgres
    password: example

If I do not include:

implementation 'org.springframework.boot:spring-boot-docker-compose'

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:

DEBUG com.zaxxer.hikari.HikariConfig.logConfiguration[1132] - jdbcUrl.........................jdbc:postgresql://127.0.0.1:15432/postgres

and the application fails to run because it's ignoring my JDBC url.

Thanks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 25, 2023
@wilkinsona
Copy link
Member

Assuming that you have an SQL database configured in ./docker-compose.yml, the behavior that you have described is expected. Service connections from containers managed by Docker Compose or Testcontainers take priority over application property-based configuration.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label May 25, 2023
@GrantGochnauer
Copy link
Author

@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!

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 25, 2023
@wilkinsona
Copy link
Member

wilkinsona commented May 25, 2023

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 POSTGRES_DB this will be used when creating the JDBC URL:

PostgresEnvironment(Map<String, String> env) {
this.username = env.getOrDefault("POSTGRES_USER", "postgres");
this.password = extractPassword(env);
this.database = env.getOrDefault("POSTGRES_DB", this.username);
}

Can you describe what you're trying to do?

@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels May 25, 2023
@GrantGochnauer
Copy link
Author

GrantGochnauer commented May 25, 2023

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!

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 25, 2023
@wilkinsona
Copy link
Member

Yes, I think that's the case.

While not very elegant, you can make it work by post-processing the JdbcConnectionDetails bean:

@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";
                }

            };
        }

    };
}

@GrantGochnauer
Copy link
Author

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?
Thanks!

@wilkinsona
Copy link
Member

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 org.springframework.boot.ignore: true. Spring Boot will then continue to use your spring.datasource.url property to connect to the Postgres instance running in the container while still calling docker compose up when you start your app.

@wilkinsona wilkinsona changed the title When including docker-compose library to runtime, spring.datasource.url is ignored in application.yml Allow the JDBC URL derived from a Docker Compose-managed container to be customized May 25, 2023
@wilkinsona wilkinsona added type: enhancement A general enhancement for: team-meeting An issue we'd like to discuss as a team to make progress and removed status: feedback-provided Feedback has been provided labels May 25, 2023
@GrantGochnauer
Copy link
Author

Thank you - I will try to ignore option. I think this will work for us.

@mhalbritter
Copy link
Contributor

mhalbritter commented May 26, 2023

Adding a label to the service

org.springframework.boot.jdbc.parameters: currentSchema=application

should work, too. We should maybe document that.

@wilkinsona
Copy link
Member

Thanks, @mhalbritter. I'd forgotten we had that 😬. I think this is just a documentation issue now.

@wilkinsona wilkinsona added type: documentation A documentation update and removed for: team-meeting An issue we'd like to discuss as a team to make progress type: enhancement A general enhancement status: waiting-for-triage An issue we've not yet triaged labels May 26, 2023
@wilkinsona wilkinsona added this to the 3.1.x milestone May 26, 2023
@wilkinsona wilkinsona changed the title Allow the JDBC URL derived from a Docker Compose-managed container to be customized Document how to add parameters to the JDBC URL created for a Docker Compose managed container May 26, 2023
@mhalbritter mhalbritter self-assigned this May 26, 2023
@GrantGochnauer
Copy link
Author

Thank you!

Adding

org.springframework.boot.jdbc.parameters: currentSchema=application

did the trick along with the environment variable: POSTGRES_DB: platform_db

@mhalbritter mhalbritter modified the milestones: 3.1.x, 3.1.1 May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

4 participants