title: Connection Refused : While Running the Docker Compose
body: <p>I am new baby to the Docker. I am using docker within the spring boot application. Below is my file .</p>
  <p>Application.properties file:</p>
  <pre><code>spring.datasource.url=jdbc:mysql://${DATABASE_HOST}:3306/${DATABASE_NAME}?allowPublicKeyRetrieval=true&amp;useSSL=true
  spring.datasource.username = root
  spring.datasource.password = root
  server.port=8443
  </code></pre>
  <p>DockerFile:</p>
  <pre><code>FROM openjdk:latest
  ADD target/app.jar app.jar
  EXPOSE 8443
  ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;app.jar&quot;, &quot;--spring.profiles.active=docker&quot;]
  </code></pre>
  <p>Docker Compose :</p>
  <pre><code>version: '3.1'
  services:
    mysqlDb:
      image: mysql:latest
      environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=springdemo
        - MYSQL_USER=root
        - MYSQL_PASSWORD=root
        - DATABASE_HOST= 192.168.0.1
      volumes:
        - /data/mysql
      ports:
      - &quot;3306:3306&quot;
      container_name: mysql-db
  
    springbootdocker:
      image: xyz/signup
      restart: always
      ports:
        - &quot;8080:8080&quot;
      depends_on:
        - mysqlDb
      links:
        - mysqlDb   
      environment:
        - DATABASE_HOST= mysqlDb
        - DATABASE_USER=root
        - DATABASE_PASSWORD=root
        - DATABASE_NAME=springdemo
        - DATABASE_PORT=3306
      healthcheck:
        test: [&quot;CMD&quot;,&quot;curl&quot;,&quot;-f&quot;,&quot;http://localhost:8080/health&quot;]
        interval: 1m30s
        timeout: 10s 
        retries: 3 
  </code></pre>
  <p><strong>1st part of Issue is Resolved :</strong>
  when i run the App , i get the following error :</p>
  <pre><code>Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  
  
  
  Caused by: java.net.UnknownHostException: ${DATABASE_HOST}
  </code></pre>
  <p><strong>1) What i am trying to Achieve -</strong></p>
  <ul>
  <li>Want to create a jar for my application.</li>
  <li>After creating a jar , i want to create the image from the jar</li>
  <li>After creating the image of the application , i want to push into the docker hub .</li>
  <li>From other server , I will run docker-compose.yml file . It will fetch the mysql and image from the docker hub and run the application in the other port as mentioned in the docker-compose.</li>
  </ul>
  <p>Hope I am clear ,what i want to acheive .</p>
  <p><strong>Here comes the problem :</strong></p>
  <ul>
  <li><p>when i am trying to run the application locally , i am able to create the jar only when ,i write this line in application.properties class.</p>
  <p><strong>spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springdemo?allowPublicKeyRetrieval=true&amp;useSSL=true</strong></p>
  </li>
  </ul>
  <p>I am able to create the jar and after that image to -&gt; upload that image to docker hub , when i am running the docker-compose able to pull that image but can't able to connect to the mysql workbench.</p>
  <ul>
  <li><p>So,I thought, I am running the docker-compose in diffrent System/Server. There ip address is different.</p>
  <p>So, I changed the url to this :</p>
  </li>
  </ul>
  <p><strong>spring.datasource.url=jdbc:mysql://192.168.0.1:3306/springdemo?allowPublicKeyRetrieval=true&amp;useSSL=true</strong></p>
  <p>where</p>
  <pre><code>192.168.0.1 - this ip is for the diffrent server, where i want to run the docker-compose file
  </code></pre>
  <p>But when i am running the spring boot application , I am unable to run the application because it's saying the almost the same error as mentioned above i.e Connection refuse . So i am unable to create the jar so , unable to create the image . So my idea fails here also.</p>
  <ul>
  <li>Then Again, One idea again come into my mind , can't we put the dynamic ip address in application.properties file,so i tried this line :</li>
  </ul>
  <p><strong>spring.datasource.url=jdbc:mysql://${DATABASE_HOST}:3306/${DATABASE_NAME}?allowPublicKeyRetrieval=true&amp;useSSL=true</strong></p>
  <p>DATABASE_HOST- you can find it in docker-compose file</p>
  <p>I thought may be this one : ${DATABASE_HOST} , will pick the value of the DATABASE_HOST from the docker-compose .</p>
  <p>But my idea failed , I am getting the error as mentioned in top while running the application. can't able to create jar ,so unable to create the image .</p>
  <p><strong>2nd Part - Issue is still Open</strong></p>
  <p>when i am able to run the docker-compose in my local terminal , and able to create the container . I can also see mysql is connected as well as my schema .
  But when i am trying to access my API using postman or web browser then , it's saying not connected</p>
  <pre><code>http://localhost:8080/dockerex
  </code></pre>
  <p>Inside my Controller Class:</p>
  <pre><code>@RestController
  @RequestMapping(value = &quot;/dockerex&quot;)
  public class SpringBootDockerController {
  
      @GetMapping
      public String check() {
          return &quot;checking&quot;;
      }
  }
  </code></pre>
  <p>when i inspect my container , ip address is coming blank : snapshot of conatiner :</p>
  <pre><code>[
   {
       &quot;Id&quot;: &quot;7664bab5ed452e3e2243e1802010240ab5a3eb817af6164e985b10e729ff4c8f&quot;,
       &quot;Created&quot;: &quot;2020-02-04T17:30:08.5351511Z&quot;,
       &quot;Path&quot;: &quot;java&quot;,
       &quot;Args&quot;: [
           &quot;-jar&quot;,
           &quot;app.jar&quot;,
           &quot;--spring.profiles.active=docker&quot;
       ],
       &quot;State&quot;: {
           &quot;Status&quot;: &quot;running&quot;
  .......................
  &quot;PortBindings&quot;: {
               &quot;8080/tcp&quot;: [
                   {
                       &quot;HostIp&quot;: &quot;&quot;,
                       &quot;HostPort&quot;: &quot;8080&quot;
                   }
               ]
           },
  </code></pre>