0

I am wondering is it possible to make docker-compose file that specifies consul port: 8500:8500 and then we have service A, service B that internal ports are from Consul added dynamiccly, and external should be static one?

For example Consul started on port 8500, then some Java Spring-boot application started on dynamic port from consul - let's say: 12345, and it was registered like that in docker-compose port as internal and external port, and application B started on dynamic port 12346 and it knows that it can communicate with application A via port 12345. Is it possible?

tryingHard
  • 1,794
  • 4
  • 35
  • 74

1 Answers1

0

The port mapping is needed for ports exposed by the container. The port on which container is listening can be mapped to any host post.

Consul is listening on 8500 port and also mapped to same port on the host. This is what your compose file will contain for consul service.

The spring boot application are the clients for consul, The client port are not listening port and are not exposed. There is no entry need for port mapping for spring boot applications.

I looked around and found a sample implementation with of auto discovery spring boot here

The docker-compose will look like this

zipkin:
  image: openzipkin/zipkin
  ports:
    - "9411:9411"

consul:
  image: consul
  ports:
    - "8400:8400"
    - "8500:8500"
    - "8600:8600"

service-b:
  image:  spring-cloud-consul-example/service-b
  links:
    - "consul"
    - "hystrix-dashboard"
    - "zipkin"

service-a:
  image:  spring-cloud-consul-example/service-a
  links:
    - "consul"
    - "service-b"
    - "hystrix-dashboard"
    - "zipkin"

admin-dashboard:
  image:  spring-cloud-consul-example/admin-dashboard
  ports:
    - "8040:8040"
  links:
    - "service-a"
    - "service-b"
    - "zuul"
    - "hystrix-dashboard"
    - "consul"

zuul:
  image:  spring-cloud-consul-example/zuul
  ports:
    - "8060:8060"
  links:
    - "consul"
    - "service-a"
    - "zipkin"

hystrix-dashboard:
  image:  spring-cloud-consul-example/hystrix-dashboard
  ports:
    - "8050:8050"
  links:
    - "consul"
asolanki
  • 1,333
  • 11
  • 18
  • I am a Windows user. I can not use `network_mode: host`, also i read i can not use `registrator` cause it uses this option. If i use docker-compose without this option with consul - my services won't subscribe to it. Let's say we have in application.yml these options: ```server: host: localhost port: ${SERVICE_PORT}``` Can you provide example working on Windows - that is docker-compose using two spring-boot services talking to each other and registrating to consul, that are ready for scaling? Also i would to use Kafka/manager in this docker-compose file. – tryingHard Jul 18 '19 at 07:20
  • You can use spring-cloud-consul if you don't want to use registrator. I have edited the answer to include a sample docker-compose file. – asolanki Jul 18 '19 at 10:26
  • But here in service a: https://github.com/yidongnan/spring-cloud-consul-example/blob/master/service-a/src/main/resources/bootstrap.yml I see static port 8080 - and i want it to be dynamic, so the port should come from Consul. – tryingHard Jul 18 '19 at 10:41
  • If you dont specify any external mapped port in the docker-compose file then the service will be mapped to any random port on the host. See this answer https://stackoverflow.com/questions/40801772/what-is-the-difference-between-docker-compose-ports-vs-expose. Then you can use registrator to register the service in consul and there is no requirement for registrator to use host network. – asolanki Jul 19 '19 at 03:46
  • I think you are writing about different port than i am. Can you provide example when you do not specify port in "bootstrap.yml" file? I do not want static port inside application as i wrote in the question, so there shouldn't be 8080 but dynamic port from consul for `service-a` and `service-b`. You are writing about external dynamic port from docker-compose. But i am writing about another port, see here: https://github.com/yidongnan/spring-cloud-consul-example/blob/master/service-a/src/main/resources/bootstrap.yml That the port `8080` is `hardcoded` i do not want that. – tryingHard Jul 31 '19 at 11:15