Docker Compose + Spring Boot + Postgres connection : <p>I have a Java Spring Boot app which works with a Postgres database. I want to use Docker for both of them. I initially put just the Postgres in Docker, and I had a <code>docker-compose.yml</code> file defined like this:</p>

<pre><code>version: '2'
services:
    db:
        container_name: sample_db
        image: postgres:9.5
        volumes:
            - sample_db:/var/lib/postgresql/data
        environment:
            - POSTGRES_PASSWORD=sample
            - POSTGRES_USER=sample
            - POSTGRES_DB=sample
            - PGDATA=/var/lib/postgresql/data/pgdata
        ports:
            - 5432:5432

volumes:
    sample_db: {}
</code></pre>

<p>Then, when I issued the commands <code>sudo dockerd</code> and <code>sudo docker-compose -f docker-compose.yml up</code>, it was starting the database. I could connect using <code>pgAdmin</code> for example, by using <code>localhost</code> as server and port <code>5432</code>. Then, in my Spring Boot app, inside the <code>application.properties</code> file I defined the following properties.</p>

<pre><code>spring.datasource.url=jdbc:postgresql://localhost:5432/sample
spring.datasource.username=sample
spring.datasource.password=sample
spring.jpa.generate-ddl=true
</code></pre>

<p>At this point I could run my Spring Boot app locally through Spring Suite, and it all was working fine. Then, I wanted to also add my Spring Boot app as Docker image. I first of all created a Dockerfile in my project directory, which looks like this:</p>

<pre><code>FROM java:8
EXPOSE 8080
ADD /target/manager.jar manager.jar
ENTRYPOINT ["java","-jar","manager.jar"]
</code></pre>

<p>Then, I entered to the directory of the project issued <code>mvn clean</code> followed by <code>mvn install</code>. Next, issued <code>docker build -f Dockerfile -t manager .</code> followed by <code>docker tag 9c6b1e3f1d5e myuser/manager:latest</code> (the id is correct). Finally, I edited my existing <code>docker-compose.yml</code> file to look like this:</p>

<pre><code>version: '2'
services:
    web:
      image: myuser/manager:latest
      ports: 
          - 8080:8080
      depends_on:
          - db
    db:
        container_name: sample_db
        image: postgres:9.5
        volumes:
            - sample_db:/var/lib/postgresql/data
        environment:
            - POSTGRES_PASSWORD=sample
            - POSTGRES_USER=sample
            - POSTGRES_DB=sample
            - PGDATA=/var/lib/postgresql/data/pgdata
        ports:
            - 5432:5432

volumes:
    sample_db: {}
</code></pre>

<p>But, now if I issue <code>sudo docker-compose -f docker-compose.yml up</code> command, the database again starts correctly, but I get errors and exit code 1 for the web app part. The problem is the connection string. I believe I have to change it to something else, but I don't know what it should be. I get the following error messages:</p>

<pre><code>web_1  | 2017-06-27 22:11:54.418 ERROR 1 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.
web_1  | 
web_1  | org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
</code></pre>

<p>Any ideas?</p>
