0

I am trying to run a docker container for a dockerized Springboot application on an AWS EC2 Centos instance. But I keep getting the following error :

org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:331) ~[postgresql-42.3.7.jar!/:42.3.7]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.3.7.jar!/:42.3.7]
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223) ~[postgresql-42.3.7.jar!/:42.3.7]
    at org.postgresql.Driver.makeConnection(Driver.java:402) ~[postgresql-42.3.7.jar!/:42.3.7]
    at org.postgresql.Driver.connect(Driver.java:261) ~[postgresql-42.3.7.jar!/:42.3.7]
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]

Caused by: java.net.UnknownHostException: host.docker.internal
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:229) ~[na:na]
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
    at org.postgresql.core.PGStream.createSocket(PGStream.java:241) ~[postgresql-42.3.7.jar!/:42.3.7]
    at org.postgresql.core.PGStream.<init>(PGStream.java:98) ~[postgresql-42.3.7.jar!/:42.3.7]
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109) ~[postgresql-42.3.7.jar!/:42.3.7]
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235) ~[postgresql-42.3.7.jar!/:42.3.7]
    ... 59 common frames omitted

This is what I have in application.properties file :

spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/dbname

This is the command I'm using to run my container :

docker run -p 8080:8888 49bf6d3bkd9d

I'm fairly new to Docker and while I know that the cause of the problem is that " host.docker.internal" is an unknown host, I don't know how to fix it.

John Dist
  • 77
  • 10
  • Where is your PostgreSQL running? – Sergio Santiago Oct 10 '22 at 22:29
  • On the EC2 instance. It is not dockerized – John Dist Oct 10 '22 at 22:37
  • 1
    Does it work? https://blog.devgenius.io/connect-locally-hosted-postgresql-from-docker-daffea720af1 – Sergio Santiago Oct 10 '22 at 22:54
  • 1
    [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) includes some discussion of the native-Linux case; `host.docker.internal` is specific to the Docker Desktop application. In an Amazon context, running the database in RDS and configuring the application to point at that is another option. – David Maze Oct 10 '22 at 23:23

0 Answers0