Spring boot app fail to link consul in docker
<p>I am trying to use Consul as discovery service, and another two spring boot app to register with Consul; and put them into docker;</p>

<p>following are my codes:
app:</p>

<pre><code>server:
  port: 3333

spring:
  application:
    name: adder
  cloud:
    consul:
      host: consul
      port: 8500
      discovery:
        preferIpAddress: true
        healthCheckPath: /health
        healthCheckInterval: 15s
        instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}
</code></pre>

<p>2 docker-compose.yml</p>

<pre><code> consul1:
  image: "progrium/consul:latest"
  container_name: "consul1"
  hostname: "consul1"
  command: "-server -bootstrap -ui-dir /ui"
 adder:
  image: wsy/adder
  ports:
   - "3333:3333"
 links:
   - consul1
 environment:
   WAIT_FOR_HOSTS: consul1:8500
</code></pre>

<p>There is another similar question <a href="https://stackoverflow.com/questions/38591295/cannot-link-consul-and-spring-boot-app-in-docker">Cannot link Consul and Spring Boot app in Docker</a>;
the answer suggests, the app should wait for consul to fully work by using depends_on, which I tried, but didn't work;</p>

<p>the error message is as following:</p>

<pre><code>adder_1    | com.ecwid.consul.transport.TransportException: java.net.ConnectException: Connection refused
adder_1    |    at com.ecwid.consul.transport.AbstractHttpTransport.executeRequest(AbstractHttpTransport.java:80) ~[consul-api-1.1.8.jar!/:na]
adder_1    |    at com.ecwid.consul.transport.AbstractHttpTransport.makeGetRequest(AbstractHttpTransport.java:39) ~[consul-api-1.1.8.jar!/:na]
</code></pre>

<hr>

<p>besides spring boot application.yml and docker-compose.yml, following is App's Dockerfile</p>

<pre><code>FROM java:8

VOLUME /tmp
ADD adder-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
ADD start.sh start.sh
RUN bash -c 'chmod +x /start.sh'
EXPOSE 3333
ENTRYPOINT ["/start.sh", " java -Djava.security.egd=file:/dev/./urandom    -jar /app.jar"]
</code></pre>

<p>and the start.sh</p>

<pre><code>#!/bin/bash

set -e

wait_single_host() {
  local host=$1
  shift
  local port=$1
  shift

  echo "waiting for TCP connection to $host:$port..."

  while ! nc ${host} ${port} &gt; /dev/null 2&gt;&amp;1 &lt; /dev/null
  do
    echo "TCP connection  [$host] not ready, will try again..."
    sleep 1
 done

  echo "TCP connection ready. Executing command [$host] now..."
}

 wait_all_hosts() {
  if [ ! -z "$WAIT_FOR_HOSTS" ]; then
   local separator=':'
   for _HOST in $WAIT_FOR_HOSTS ; do
      IFS="${separator}" read -ra _HOST_PARTS &lt;&lt;&lt; "$_HOST"
      wait_single_host "${_HOST_PARTS[0]}" "${_HOST_PARTS[1]}"
    done
   else
     echo "IMPORTANT : Waiting for nothing because no $WAIT_FOR_HOSTS env var defined !!!"
   fi
  }

  wait_all_hosts

  exec $1
</code></pre>