title: Docker Compose + Spring Boot + Postgres connection, resulting in error
body: <p>I have a Java Spring Boot app which works with a Postgres database. I want to use Docker for both of them. Initially, I created a <code>docker-compose.yml</code> file as given below:</p>
  <pre><code>version: '3'
  services:
    db:
      container_name: sample_db
      image: postgres
      volumes:
        - sample_db:/var/lib/postgresql/data
      ports:
        - &quot;5432:5432&quot;
      environment:
        - POSTGRES_DB=&quot;employee&quot;
        - POSTGRES_USER=&quot;user&quot;
        - POSTGRES_PASSWORD=&quot;password&quot;
        - PGDATA=/var/lib/postgresql/data/pgdata
  
    web:
      build: .
      expose:
        - &quot;8080&quot;
      depends_on:
        - db
  volumes:
    sample_db: {}
  </code></pre>
  <p>Then, in my Spring Boot app, inside the application.properties file I defined the following properties.</p>
  <pre><code>spring.datasource.url=jdbc:postgresql://db:5432/employee
  spring.datasource.username=user
  spring.datasource.password=password
  </code></pre>
  <p>along with this,I created a Dockerfile in my project directory, which looks like this:</p>
  <pre><code>FROM openjdk:8-jdk-alpine
  EXPOSE 8080    
  ARG JAR_FILE=target/employee-0.0.1-SNAPSHOT.jar    
  COPY ${JAR_FILE} employee-demo.jar    
  ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;employee-demo.jar&quot;]
  </code></pre>
  <p>I issued these commands and ended up in the error as given below</p>
  <blockquote>
  <p>mvn clean package</p>
  <p>docker-compose up</p>
  </blockquote>
  <p>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> Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
  web_1  |    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.5.jar!/:42.2.5]
  web_1  |    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar!/:42.2.5]
  web_1  |    at org.postgresql.jdbc.PgConnection.&lt;init&gt;(PgConnection.java:195) ~[postgresql-42.2.5.jar!/:42.2.5]
  web_1  |    at org.postgresql.Driver.makeConnection(Driver.java:454) ~[postgresql-42.2.5.jar!/:42.2.5]
  web_1  |    at org.postgresql.Driver.connect(Driver.java:256) ~[postgresql-42.2.5.jar!/:42.2.5]
  web_1  |    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
  web_1  |    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
  web_1  |    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
  web_1  |    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
  web_1  |    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
  web_1  |    at com.zaxxer.hikari.pool.HikariPool.&lt;init&gt;(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
  web_1  |    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
  web_1  |    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
  web_1  |    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
  web_1  |    at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
  web_1  |    ... 44 common frames omitted
  web_1  | Caused by: java.net.UnknownHostException: sample_db
  web_1  |    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_181]
  web_1  |    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_181]
  web_1  |    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_181]
  web_1  |    at org.postgresql.core.PGStream.&lt;init&gt;(PGStream.java:70) ~[postgresql-42.2.5.jar!/:42.2.5]
  web_1  |    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) ~[postgresql-42.2.5.jar!/:42.2.5]
  web_1  |    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ~[postgresql-42.2.5.jar!/:42.2.5]
  web_1  |    ... 58 common frames omitted
  </code></pre>
  <p>Am new to docker, If any issues please inform</p>