1

I have a problem with Docker-compose. On my machine, I have a Ubuntu. And, when I deploy my app into container, I see that exception:

        at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:307) ~[liquibase-core-3.6.3.jar!/:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
        ... 26 common frames omitted
Caused by: 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.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280) ~[postgresql-42.2.5.jar!/:42.2.5]
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar!/:42.2.5]
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.5.jar!/:42.2.5]
        at org.postgresql.Driver.makeConnection(Driver.java:454) ~[postgresql-42.2.5.jar!/:42.2.5]
        at org.postgresql.Driver.connect(Driver.java:256) ~[postgresql-42.2.5.jar!/:42.2.5]
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
        at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:302) ~[liquibase-core-3.6.3.jar!/:na]
        ... 28 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
        at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
        at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:240) ~[na:na]
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
        at java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
        at org.postgresql.core.PGStream.<init>(PGStream.java:70) ~[postgresql-42.2.5.jar!/:42.2.5]
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) ~[postgresql-42.2.5.jar!/:42.2.5]
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ~[postgresql-42.2.5.jar!/:42.2.5]
        ... 40 common frames omitted

I have configuration of PostgreSQL:


# "local" is for Unix domain socket connections only
local   all         all                               trust

# IPv4 local connections:
host    all         all         127.0.0.1/32          trust
host    all         all         100.200.300.50/32       trust

And that option set "listen_addresses = 'localhost'";

Docker-compose config:

version: '3'
services:

  web:
service
    image: webserviceimage
    ports:
      - 8080:8080 
    depends_on:
      - db 
      - redis
    environment:
      POSTGRES_URL:
      POSTGRES_USER:
      POSTGRES_PASSWORD:

  redis:
    image: "redis:alpine"
    restart: unless-stopped
    environment:
      REDIS_URL: redis:6379
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD:
      POSTGRES_USER:

How I can resolve it?

Sam Samovich
  • 79
  • 1
  • 2
  • 8
  • You’re missing configuration for your application to reach the database container: 0.0.0.0 is a special IPv4 address for “everywhere” and it’s not a normal address for outbound connections. Conversely, if you’ve configured the database container to only accept connections from `localhost` then it can’t accept connections from other containers; switch back to the default configuration. – David Maze Sep 11 '19 at 15:58
  • I need to switch "listen_addresses = 'localhost'" on "listen_addresses = '*"; and after that I will add all all 0.0.0.0/32 trust, I am right? – Sam Samovich Sep 11 '19 at 16:11

2 Answers2

1

Step: 1 Create Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Step:2 Run command to create docker file

docker build -t webapp:latest -f Dockerfile .

Step:3 Add docker image into docker-compose

version: '3'
services:
  web:
service
    image: webapp:latest
    ports:
      - 8080:8080 
    depends_on:
      - db 
      - redis
    environment:
      POSTGRES_URL:
      POSTGRES_USER:
      POSTGRES_PASSWORD:

  redis:
    image: "redis:alpine"
    restart: unless-stopped
    environment:
      REDIS_URL: redis:6379
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD:
      POSTGRES_USER:

Notes : web app environment variable user into application.properties file like

spring.datasource.url = ${POSTGRES_URL}:${POSTGRES_PORT}/"nameofDB"

Mayur Kothari
  • 254
  • 2
  • 4
0

Make sure you have Postgres configured to listen on all available IP addresses. The configuration file postgresql.conf (typically located in /var/lib/postgresql/data/postgresql.conf) must have:

listen_addresses = '*'

(this is the default configuration). More here

The file pg_hba.conf contains the configuration for the host based authentication. The posted configuration means that connections coming from the IPs with the defined mask or unix sockets are trusted (don't require authentication). You don't have to change this file unless you have specific security requirements. By default remote clients must supply an MD5-encrypted password for authentication:

host all all all md5

For more info see The pg_hba.conf File

What was suggested in the comments is to change the URL of the database in the configuration of the spring boot application. From the logs the application tries to connect to 0.0.0.0 which is not a valid outbound address (see here). Change the URL in the application properties to something like this:

spring.datasource.url=jdbc:postgresql://db:5432/<database_name>
spring.datasource.username=<database_user>
spring.datasource.password=<password>

db is the name of the database container which can be resolved by the service container to the correct database IP address.

Hope it helps

b0gusb
  • 4,283
  • 2
  • 14
  • 33