title: Spring Docker container cannot access Postgres Docker container
body: <p>The Dockerfile of my spring-boot app:</p>
  
  <pre><code>FROM openjdk:8-jdk-alpine
  VOLUME /tmp
  COPY target/media-0.0.1-SNAPSHOT.jar app.jar
  ENTRYPOINT ["java", "-jar", "/app.jar"]
  </code></pre>
  
  <p><code>application.yml</code></p>
  
  <pre><code>spring:
    datasource:
      url: jdbc:postgresql://localhost:5432/media
      username: postgres
      password: postgres
      hikari:
        connectionTimeout: 30000
  </code></pre>
  
  <p>and here is the <code>docker-compose.yml</code>:</p>
  
  <pre><code>version: '3'
  services:
    db:
      image: postgres
      ports:
        - "5432:5432"
      environment:
        POSTGRES_DB: media
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
  
    app:
      build:
        context: ./
        dockerfile: Dockerfile
      depends_on:
        - db
      ports:
        - "8080:8080"
  </code></pre>
  
  <p>Running <code>docker-compose up --build</code> results in:</p>
  
  <blockquote>
    <p>app_1  | org.postgresql.util.PSQLException: Connection to 0.0.0.0:5432
    refused. Check that the hostname and port are correct and that the
    postmaster is accepting TCP/IP connections. app_1 </p>
  </blockquote>
  
  <p>My guess is that the spring app tries to connect to postgres before postgres is ready, but I get the following log:</p>
  
  <blockquote>
    <p>db_1   | 2019-05-18 19:05:53.692 UTC [1] LOG:  database system is
    ready to accept connections</p>
  </blockquote>