spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Docker compose: add configuration setting for selecting running services to bind

Open dsyer opened this issue 2 years ago • 17 comments

I have a docker-compose.yml that starts multiple services but I only want to bind to one (or some) of them. It is possible to do that currently by re-arranging the compose file into "profiles" and setting spring.docker.compose.profiles.active. But I might not be able to do that for an arbitrary set of services (and I might not want or be able to edit the compose file either), so it would be nice to be able to simply list (or pattern match) the services to bind to.

dsyer avatar Apr 18 '23 12:04 dsyer

You can ignore services by labelling them with org.springframework.boot.ignore. That will require editing your docker-compose.yml file. Why might you not be able to do that?

wilkinsona avatar Apr 18 '23 12:04 wilkinsona

I didn’t know about that, and it might be useful, but not in general. Imagine I want to switch between 2 services for independent tests, for example - it would be awkward to have to edit the compose config in between test invocations.

dsyer avatar Apr 18 '23 13:04 dsyer

The docker compose support isn't aimed at tests. It's intended for use when running the whole app during development as a replacement for manually running docker compose up and then configuring properties to connect to the resulting service.

I'm not opposed to adding something but I don't think there's a compelling use case yet.

wilkinsona avatar Apr 18 '23 14:04 wilkinsona

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar Apr 25 '23 14:04 spring-projects-issues

I don't know why the use case I mentioned isn't enough to at least keep the issue open.

dsyer avatar Apr 25 '23 21:04 dsyer

I was thinking about this one the other day and I wonder if we could offer a way to apply labels the application.yaml. Something like:

spring:
  docker:
    compose:
      labels:
        my-service:
          org.springframework.boot.ignore: true

philwebb avatar Apr 26 '23 00:04 philwebb

That would work, but why does it work better than simply listing the services you want to bind? To me it makes sense that I am mirroring the docker-compose command line (like with profiles). "I tell you where to find the YAML and what arguments to add to docker-compose, you run it for me." Labels seem like an unnecessary (in this case) indirection.

dsyer avatar Apr 26 '23 12:04 dsyer

The main reason behind my thinking was we could support the other labels (such as org.springframework.boot.readiness-check.tcp.disable), but listing services is probably easier to follow.

philwebb avatar Apr 26 '23 18:04 philwebb

@philwebb @wilkinsona I preferred to add a feedback here for the spring-boot-docker-compose auto configuration, but if it's needed I can create an issue. How to start some containers and not binding to the spring context (within any ConnectionDetails or RunningService) ? I would like to start Keycloak and it's Postgres db when I start my Spring app but my own service doesn't have any JDBC connection (neither dependency added in pom) but it still tries to create a connection with Keycloak's Postgres instance which shouldn't be done automatically. I read the docs but the only info was about org.springframework.boot.ignore: true which disables totally the startup of the container.

adamalexandru4 avatar May 22 '23 21:05 adamalexandru4

@adamalexandru4 Perhaps we need to improve our documentation. Adding aorg.springframework.boot.ignore label should not stop Spring Boot from running docker compose up. Did you try that and find it wasn't the case? If so could you please open a new issue.

philwebb avatar May 22 '23 22:05 philwebb

Big +1 to this

That will require editing your docker-compose.yml file. Why might you not be able to do that?

In my case, we have a few different repos that each define their own compose file for how to run that service.

Our compose file for ServiceA has something like

include:
    - ../serviceB/docker-compose.yml

...

This means that to run this particular compose file I'd need to add ignore labels to the other repos compose file, which I don't want because it would mean the other service won't create service connections.

Specifying which containers to bind to will also mean changes to service names or the addition/removal of services in the included compose does not affect my settings unless I explicitly bind to external containers

mroche89 avatar Apr 22 '24 02:04 mroche89

I do like what @dsyer is proposing

aldex32 avatar Jul 11 '24 09:07 aldex32