skip to Main Content

I want to build my spring boot app with a Postgres db in a docker container with docker compose. I got an error when I build the docker compose container. The spring app cannot connect to the Postgres db. The Postgres container run as it should and I can create a connection with a usually db-client (dbeaver).

Here are my configuration files:

1. application.yml

server:
  port: 8081
  error:
    include-message: always
    include-binding-errors: always
spring:
  datasource:
    password: password
    url: jdbc:postgresql://postgres:5432/trainingdb
    username: user
  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
    show-sql: true

2. Dockerfile

FROM openjdk:11
ADD target/training-service-1.0.0.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]

3. docker-compose.yml

version: '1'
services:
  app:
    container_name: springboot
    image: springboot
    build: ./
    depends_on:
      - postgres
    ports:
      - "8081:8081"
  postgres:
    image: postgres:latest
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=trainingdb

first I run
./mvnw clean package -DskipTests

to build the jar file and then I run the
docker compose up

little snippet to the log messages:

springboot                   |  at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.24.jar!/:42.2.24]
springboot                   |  at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:225) ~[postgresql-42.2.24.jar!/:42.2.24]
springboot                   |  at org.postgresql.Driver.makeConnection(Driver.java:465) ~[postgresql-42.2.24.jar!/:42.2.24]
springboot                   |  at org.postgresql.Driver.connect(Driver.java:264) ~[postgresql-42.2.24.jar!/:42.2.24]
springboot                   |  at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
springboot                   |  at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
springboot                   |  at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
springboot                   |  at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
springboot                   |  at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
springboot                   |  at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
springboot                   |  at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]
springboot                   |  at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.32.Final.jar!/:5.4.32.Final]
springboot                   |  at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.32.Final.jar!/:5.4.32.Final]
springboot                   |  at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.4.32.Final.jar!/:5.4.32.Final]
springboot                   |  ... 44 common frames omitted
springboot                   | Caused by: java.net.UnknownHostException: postgresql
springboot                   |  at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220) ~[na:na]
springboot                   |  at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
springboot                   |  at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
springboot                   |  at org.postgresql.core.PGStream.createSocket(PGStream.java:231) ~[postgresql-42.2.24.jar!/:42.2.24]
springboot                   |  at org.postgresql.core.PGStream.<init>(PGStream.java:95) ~[postgresql-42.2.24.jar!/:42.2.24]
springboot                   |  at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98) ~[postgresql-42.2.24.jar!/:42.2.24]
springboot                   |  at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213) ~[postgresql-42.2.24.jar!/:42.2.24]
springboot                   |  ... 58 common frames omitted
springboot                   | 
springboot exited with code 1

The log messages from my postgres container:


PostgreSQL Database directory appears to contain a database; Skipping initialization


2022-01-30 07:39:04.321 UTC [1] LOG:  starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

2022-01-30 07:39:04.321 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432

2022-01-30 07:39:04.321 UTC [1] LOG:  listening on IPv6 address "::", port 5432

2022-01-30 07:39:04.327 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

2022-01-30 07:39:04.338 UTC [27] LOG:  database system was shut down at 2022-01-30 07:38:38 UTC

2022-01-30 07:39:04.349 UTC [1] LOG:  database system is ready to accept connections

2022-01-30 07:42:16.689 UTC [45] LOG:  invalid length of startup packet

2022-01-30 07:42:16.709 UTC [46] LOG:  invalid length of startup packet

2022-01-30 07:42:16.726 UTC [47] LOG:  invalid length of startup packet

2022-01-30 07:42:39.376 UTC [1] LOG:  received fast shutdown request

2022-01-30 07:42:39.379 UTC [1] LOG:  aborting any active transactions

2022-01-30 07:42:39.381 UTC [1] LOG:  background worker "logical replication launcher" (PID 33) exited with exit code 1

2022-01-30 07:42:39.386 UTC [28] LOG:  shutting down

2022-01-30 07:42:39.430 UTC [1] LOG:  database system is shut down

2022-01-30 07:45:33.835 UTC [1] LOG:  starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

2022-01-30 07:45:33.835 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432

2022-01-30 07:45:33.836 UTC [1] LOG:  listening on IPv6 address "::", port 5432

2022-01-30 07:45:33.841 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

2022-01-30 07:45:33.852 UTC [26] LOG:  database system was shut down at 2022-01-30 07:42:39 UTC

2022-01-30 07:45:33.861 UTC [1] LOG:  database system is ready to accept connections

2022-01-30 07:48:44.550 UTC [1] LOG:  received fast shutdown request

2022-01-30 07:48:44.559 UTC [1] LOG:  aborting any active transactions

2022-01-30 07:48:44.564 UTC [1] LOG:  background worker "logical replication launcher" (PID 32) exited with exit code 1

2022-01-30 07:48:44.572 UTC [27] LOG:  shutting down

2022-01-30 07:48:44.630 UTC [1] LOG:  database system is shut down

2022-01-30 07:49:05.986 UTC [1] LOG:  starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

2022-01-30 07:49:05.986 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432

2022-01-30 07:49:05.988 UTC [1] LOG:  listening on IPv6 address "::", port 5432

2022-01-30 07:49:05.995 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

2022-01-30 07:49:06.004 UTC [26] LOG:  database system was shut down at 2022-01-30 07:48:44 UTC

2022-01-30 07:49:06.035 UTC [1] LOG:  database system is ready to accept connections


PostgreSQL Database directory appears to contain a database; Skipping initialization



PostgreSQL Database directory appears to contain a database; Skipping initialization

``

 command to start up the containers. Is there any error in my spring boot configuration ?

2

Answers


  1. I think the problem is your jdbc URL: jdbc:postgresql://postgres:5432/trainingdb

    can you try this URL instead: jdbc:postgresql://localhost:5432/trainingdb

    Login or Signup to reply.
  2. Can you check on the log, whether postgre container is up and running?

    Also please check the container status by using the command ‘docker ps’ and check for the container name ‘postgres’.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search