Q:

Cannot connect dockerized spring boot app to dockerized postgre sql

I have a docker-compose.yml with two services:
version: '2'
services:

  stuffer:
    container_name: stuffer_container
    build: .
    ports:
     - "8080:8080"
    environment:
     - spring.profiles.active=dev
    depends_on:
     - postgreDB

  postgreDB:
    container_name: postgreDB_container
    image: "postgres:9.6.2"
    ports:
     - "5432:5432"
    expose:
     - "5432"
    environment:
     - POSTGRES_DB=stuffer
     - POSTGRES_USER=postgres
     - POSTGRES_PASSWORD=root123
    volumes:
     - /home/iwaneez/postgreData:/var/lib/postgresql/data

my app has application-dev.properties:
## Datasource config
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://postgreDB:5432/stuffer
spring.datasource.username=postgres
spring.datasource.password=root123

When I run them both using 

docker-compose up

I get connection refused exception:
stuffer_container | Caused by: org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
stuffer_container |     at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:60) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:192) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
stuffer_container |     at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:92) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
stuffer_container |     at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:83) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
stuffer_container |     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
stuffer_container |     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
stuffer_container |     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
stuffer_container |     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
stuffer_container |     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     ... 61 common frames omitted
stuffer_container | Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
stuffer_container |     at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:46) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     ... 71 common frames omitted
stuffer_container | Caused by: org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
stuffer_container |     at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:138) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:32) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.Driver.makeConnection(Driver.java:393) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.Driver.connect(Driver.java:267) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:732) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:664) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:479) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     ... 72 common frames omitted
stuffer_container | Caused by: java.net.ConnectException: Connection refused (Connection refused)
stuffer_container |     at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_131]
stuffer_container |     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_131]
stuffer_container |     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_131]
stuffer_container |     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_131]
stuffer_container |     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_131]
stuffer_container |     at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_131]
stuffer_container |     at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_131]
stuffer_container |     at java.net.Socket.<init>(Socket.java:434) ~[na:1.8.0_131]
stuffer_container |     at java.net.Socket.<init>(Socket.java:211) ~[na:1.8.0_131]
stuffer_container |     at org.postgresql.core.PGStream.<init>(PGStream.java:62) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:76) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     ... 91 common frames omitted

How can I connect the application to postgre both running in docker?
I also tried running ubuntu image as third service and could successfully connect to db using
telnet postgreDB 5432

so it looks like it is visible, but app still cannot connect to it.
Changing datasource url property from postgreDB to localhost like
spring.datasource.url=jdbc:postgresql://localhost:5432/stuffer

lets me connect the application to postgre from outside the docker, but I need them both running in docker.

A:

I managed to solve the problem by running
docker-compose build 

before running 
docker-compose up 

The problem was that there was an old image pointing to postgre through localhost. And althought I changed the host in config to postgreDB it wasn't built during up command as I thought from the command manual.
So no change was reflected in result.

