DaeYoung
DaeYoung

Reputation: 15

Postgresql Connection Refused in docker container, But Success on server

I try to connect spring app with postgresql.

When I run my app directly on server(not container), Connection success.

java -jar app.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

2021-07-05 14:10:58.348  INFO 334534 --- [           main] com.studb.poemNote.PoemNoteApplication   : Starting PoemNoteApplication v1 using Java 11.0.11 on ip-172-31-3-67 with PID 334534 (/home/ubuntu/poemnote/poemNote/target/poemNote-1.jar started by ubuntu in /home/ubuntu/poemnote/poemNote)
2021-07-05 14:10:58.355  INFO 334534 --- [           main] com.studb.poemNote.PoemNoteApplication   : No active profile set, falling back to default profiles: default
2021-07-05 14:11:00.447  INFO 334534 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2021-07-05 14:11:00.498  INFO 334534 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 46 ms. Found 0 JDBC repository interfaces.
2021-07-05 14:11:01.903  INFO 334534 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9000 (http)
2021-07-05 14:11:01.936  INFO 334534 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-07-05 14:11:01.936  INFO 334534 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-07-05 14:11:02.095  INFO 334534 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-07-05 14:11:02.095  INFO 334534 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3595 ms
2021-07-05 14:11:02.945  INFO 334534 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-07-05 14:11:03.197  INFO 334534 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-07-05 14:11:04.497  INFO 334534 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-07-05 14:11:04.762  INFO 334534 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2021-07-05 14:11:05.241  INFO 334534 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9000 (http) with context path ''
2021-07-05 14:11:05.267  INFO 334534 --- [           main] com.studb.poemNote.PoemNoteApplication   : Started PoemNoteApplication in 8.396 seconds (JVM running for 9.694)
^C2021-07-05 14:11:13.986  INFO 334534 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

But when i run container, connection fail like below

docker run -p port:port

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

2021-07-05 14:12:37.130  INFO 1 --- [           main] com.studb.poemNote.PoemNoteApplication   : Starting PoemNoteApplication v1 using Java 11.0.11 on de04c6a415a5 with PID 1 (/app/BOOT-INF/classes started by root in /app)
2021-07-05 14:12:37.139  INFO 1 --- [           main] com.studb.poemNote.PoemNoteApplication   : No active profile set, falling back to default profiles: default
2021-07-05 14:12:38.997  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2021-07-05 14:12:39.055  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 47 ms. Found 0 JDBC repository interfaces.
2021-07-05 14:12:40.351  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9000 (http)
2021-07-05 14:12:40.383  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-07-05 14:12:40.383  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-07-05 14:12:40.516  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-07-05 14:12:40.516  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3260 ms
2021-07-05 14:12:41.248  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-07-05 14:12:52.405 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315) ~[postgresql-42.2.19.jar:42.2.19]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.19.jar:42.2.19]
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223) ~[postgresql-42.2.19.jar:42.2.19]
    at org.postgresql.Driver.makeConnection(Driver.java:465) ~[postgresql-42.2.19.jar:42.2.19]
    at org.postgresql.Driver.connect(Driver.java:264) ~[postgresql-42.2.19.jar:42.2.19]
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.5.jar:na]

It's my dockerfile

FROM  adoptopenjdk:11-jre-hotspot as builder

WORKDIR app
ARG JAR_FILE=target/*jar
COPY ${JAR_FILE} app.jar
RUN java -Djarmode=layertools -jar app.jar extract

FROM adoptopenjdk:11-jre-hotspot
WORKDIR app
COPY --from=builder app/dependencies/ ./
COPY --from=builder app/spring-boot-loader/ ./
COPY --from=builder app/snapshot-dependencies/ ./
COPY --from=builder app/application/ ./

EXPOSE 9000

ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

I can't understand why it does...

I try run container '--network host' but it's failed.

Upvotes: 0

Views: 3561

Answers (1)

jsattler
jsattler

Reputation: 538

Postgresql running inside container on the same host

In case postgresql is also running inside a container of the same host, you need to make sure that they share the same network and that the containers can reach each other.

Check configured networks of the container:

docker inspect <container> -f "{{json .NetworkSettings.Networks }}"

Check if you can reach one container from the another:

docker exec -u 0 <container1> ping <container2> -c2

In case they are not on the same network, you need to create the network first and then connect the containers to it:

docker network create <network>

and

docker network connect <network> <container>

Postgresql running directly on the host

In case postgresql is running directly on the host where your docker container is running, you need to make sure that you can reach the host system from your container. Try to add the following command when you start the container

--add-host=host.docker.internal:host-gateway

Useful Links:

Upvotes: 1

Related Questions