I'm running a number of Spring Boot applications (with Actuator) that use Consul for service discovery. If I start the applications using docker-compose, specifying both the Host and Container port for each of the containers then they register correctly with Consul and pretty quickly get marked as healthy.
spring:
application:
name: myapp-A
profiles:
active: default
cloud:
consul:
enabled: true
host: consul
port: 8500
discovery:
prefer-ip-address: true
healthCheckUrl: http://docker-host:${server.port}/health
The host address values for "consul" and "docker-host" are passed in as environment variables in the docker-compose yml file on startup and in simplistic terms relate to the ip address of the docker host machine (I am running Consul on the same server).
Please also note the use of the :
${server.port}
variable to tell Consul how to connect back to the container to check its health (it just uses Actuator in this respect). This needs to be an externally accessible address and port so that Consul can check the services.
This all works fine when I specify both the Host and Container port numbers in the docker-compose file for the containers.
I have got to the point where I want to scale up a few of the containers, but when I try to execute the
docker-compose up -d --scale myapp-A=5
command it tells me that it's not possible as the Host port has been specified. If I change the docker-compose yml file to only define the container port, and leave the Host port to be dynamically assigned it all then goes wrong as Consul (running externally) gets given the wrong URL to connect to and marks the containers as being unhealthy.
The question I have is how do I tell Consul the dynamically and "externally" exposed port number to use from my app running inside the container?
Is there some way to get the port number and pass that into the container at the point of creation?
I'd like to try and crack this using Compose if I can (rather than using Swarm, or by running Consul in another container in the same composed set of containers).