Search code examples
javadockerspring-cloudmicroservicesconsul

Java Spring will not talk to Consul when run as a Docker container


I am trying to solve what I believe is a common use case for running micro services. In this case I am testing consul with a spring cloud application. I am trying to test consul in two different ways. The first of which is running in a docker container and the other is running on the docker host machine. I am then attempting to start a spring cloud container that will talk with either consul example.

I have been unable to make the spring cloud application talk to consul when the spring cloud application is run as a docker container. When the spring cloud application is run with the host networking mode it works as it can resolve the localhost ports, but this is not an acceptable solution if I wish to run multiple instances of the image.

An example of my docker compose file when running both services as containers is shown below. Here I am attempting to set the consul uri in spring cloud through the environment variables, but have been unable to get it to work using a variety of configurations. If anyone could point to an example of these functions working together that would be immensely helpful.

 consul1:
  image: progrium/consul
  ports:
    - "8400:8400"
    - "8500:8500"
    - "8600:53/udp"
    - "8600:53/tcp"
  environment:
      GOMAXPROCS: 100
      entrypoint: "/bin/consul"
      hostname: consul
      command: agent -log-level=debug -server -config-dir=/config -bootstrap -ui-dir /ui

simpletest:
    build: simpletest
    hostname: simpletest
    environment:  
      JAVA_OPTS: "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n -Dspring.cloud.consul.host=consul1"
    ports:
     - 39041:7051
     - 39052:7055
#     d2fdockerroot_consul1_1 consul
#    links:
#     - consul1

Solution

  • Here you have an example of a brewery system - https://github.com/spring-cloud-samples/brewery. One of the files is a docker-compose file for CONSUL.

    https://github.com/spring-cloud-samples/brewery/blob/master/docker-compose-CONSUL.yml

    Check out all the application-consul.yaml files that are inside the codebase to see how to set up the Spring Boot apps to talk to consul.

    Example: https://github.com/spring-cloud-samples/brewery/blob/master/aggregating/src/main/resources/application-consul.yaml

    In case of any issues write here or go to spring-cloud gitter https://gitter.im/spring-cloud/spring-cloud