简体   繁体   中英

Docker container crashes at startup on a remote server, how to run correctly?

I have the Spring Boot app and the following Dockerfile:

FROM openjdk:16

COPY build/libs/*.jar tg-bot.jar

ENTRYPOINT java -Dfile.encoding=UTF-8 -jar tg-bot.jar
EXPOSE 8080

The application runs correctly in Docker on my PC:

docker run -p 8080:8080 tg-bot

Then I pushed image to my Dockerhub repository, pull it in Docker on my VPS and tried to run it with the same command:

docker run -p 8080:8080 tg-bot

When the Spring Boot app starts, an error occurs connecting to the PostgreSQL database:

Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
  at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315) ~[postgresql-42.2.23.jar!/:42.2.23]
  at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.23.jar!/:42.2.23]
  at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223) ~[postgresql-42.2.23.jar!/:42.2.23]
  at org.postgresql.Driver.makeConnection(Driver.java:465) ~[postgresql-42.2.23.jar!/:42.2.23]
  at org.postgresql.Driver.connect(Driver.java:264) ~[postgresql-42.2.23.jar!/:42.2.23]
  at org.springframework.jdbc.datasource.SimpleDriverDataSource.getConnectionFromDriver(SimpleDriverDataSource.java:144) ~[spring-jdbc-5.3.9.jar!/:5.3.9]
  at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:205) ~[spring-jdbc-5.3.9.jar!/:5.3.9]
  at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:169) ~[spring-jdbc-5.3.9.jar!/:5.3.9]
  at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:272) ~[liquibase-core-4.3.5.jar!/:na]
  ... 26 common frames omitted
Caused by: java.net.NoRouteToHostException: No route to host
  at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
  at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:669) ~[na:na]
  at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549) ~[na:na]
  at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
  at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333) ~[na:na]
  at java.base/java.net.Socket.connect(Socket.java:645) ~[na:na]
  at org.postgresql.core.PGStream.createSocket(PGStream.java:231) ~[postgresql-42.2.23.jar!/:42.2.23]
  at org.postgresql.core.PGStream.<init>(PGStream.java:95) ~[postgresql-42.2.23.jar!/:42.2.23]
  at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98) ~[postgresql-42.2.23.jar!/:42.2.23]
  at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213) ~[postgresql-42.2.23.jar!/:42.2.23]
  ... 34 common frames omitted

This is my application.yml with db settings:

spring:
  application:
    name: tg-bot
  r2dbc:
    url: r2dbc:postgresql://postgresql.j5321585.myjino.ru:5432/j5321585_tg_db
    username: ********
    password: ********
  liquibase:
    enabled: true
    change-log: classpath:db/scripts/changelog-master.xml
    url: jdbc:postgresql://postgresql.j5321585.myjino.ru:5432/j5321585_tg_db
    user: ********
    password: ********

There are no errors when starting the container locally with the same application.yaml. What could be the problem?

This depends on how you are running your Postgres Application.

If Postgres is running on a separate server make sure that server is accessible from within your Application Docker container.

If Postgres is running as a Docker container on same server, you can start both your application and Postgres Docker container as a docker-compose file. In that you can specify the host or ip-address for the Postgres docker container and use the same in your application code.

Hope this is helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM