Search code examples
dockerspring-bootdocker-composespring-cloudconsul

Cannot link Consul and Spring Boot app in Docker


I have a Spring Boot app with the following config:

spring:
  thymeleaf:
    cache: false
  cloud:
    consul:
      host: consul
      port: 8500
      discovery:
        prefer-ip-address: true
        instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}

that I want to run with docker-compose(Docker 1.11.2, docker-compose 1.7.1):

consul:
  image: progrium/consul:latest
  container_name: consul
  hostname: consulhost
  ports:
    - "8400:8400"
    - "8500:8500"
    - "8600:53"
  command: "-server -bootstrap-expect 1 -ui-dir /ui"

collector-server:
  container_name: collector-server
  image: io.thesis/collector-server
  ports:
    - "9090:9090"
  links:
    - consul:consul

Unfortunately that does not work, I get: com.ecwid.consul.transport.TransportException: java.net.ConnectException: Connection refused.

I have absolutely no idea why it can't connect to Consul, because I can connect to other systems , e.g. rabbitmq in other applications exactly this way.

Thank you for any ideas!


Solution

  • If you're trying to connect immediately when the container starts it's possible that consul is not ready to receive connections yet.

    You need to write an entrypoint script to wait for the connection to be available, or even just retry the connection a few times. See https://docs.docker.com/compose/startup-order/ for more information.