无法通过Play框架服务器连接到postgresql docker容器

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (47)

这是我的docker-compose.yml档案

version: '3'
services:
    polymer:
        restart: always
        container_name: svcss_container
        build: 
            context: ./svcss
            args:
                POSTGRES_JDBC_URL: "jdbc:postgresql://127.0.0.1:5432/mydb_db"
                POSTGRES_USER: "mydb_user"
                POSTGRES_PASSWORD: "mydb_pass"
        ports:
            - "9090:9090"
        depends_on:
            - db
        links:
            - db:postgreshost

    db:
        container_name: pg_container
        restart: always
        image: postgres
        environment:
            POSTGRES_USER: "mydb_user"
            POSTGRES_DB: "mydb_db"
            POSTGRES_PASSWORD: "mydb_pass"
        ports:
            - "5432:5432"

这就是我制作生产的方式 svc/ sbt dist set -x rm -rf svc/ unzip -d svc target/universal/*-1.0-SNAPSHOT.zip mv svc/*/* svc/ rm svc/bin/*.bat mv svc/bin/* svc/bin/start

Dockerfile内部./svcss的文件夹,其预计所有sbt dist从提取的二进制文件target/universe/*.zip./svcss/svc/文件夹中。

FROM openjdk:8-jre
COPY svc /svc

ARG POSTGRES_JDBC_URL
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD

ENV POSTGRES_JDBC_URL $POSTGRES_JDBC_URL
ENV POSTGRES_USER $POSTGRES_USER
ENV POSTGRES_PASSWORD $POSTGRES_PASSWORD

EXPOSE 9090
CMD /svc/bin/start -Dplay.http.secret.key="mysecretkeyishere123123123123"

但是在运行时使用

docker-compose up

我可以在Postgres使用某个客户端从我的计算机连接到容器内的数据库。但svcss_container不知怎的,不能进行演变

svcss_container | Oops, cannot start the server.
svcss_container | java.sql.SQLTransientConnectionException: db - Connection is not available, request timed out after 1007ms.
svcss_container |   at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:548)
svcss_container |   at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:186)
svcss_container |   at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:145)
svcss_container |   at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:83)
svcss_container |   at slick.jdbc.hikaricp.HikariCPJdbcDataSource.createConnection(HikariCPJdbcDataSource.scala:14)
svcss_container |   at play.api.db.slick.evolutions.internal.DBApiAdapter$DatabaseAdapter.getConnection(DBApiAdapter.scala:57)
svcss_container |   at play.api.db.slick.evolutions.internal.DBApiAdapter$DatabaseAdapter.getConnection(DBApiAdapter.scala:60)
svcss_container |   at play.api.db.evolutions.DatabaseEvolutions.databaseEvolutions(EvolutionsApi.scala:135)
svcss_container |   at play.api.db.evolutions.DatabaseEvolutions.scripts(EvolutionsApi.scala:113)
svcss_container |   at play.api.db.evolutions.DatabaseEvolutions.scripts(EvolutionsApi.scala:128)
svcss_container |   at play.api.db.evolutions.DefaultEvolutionsApi.scripts(EvolutionsApi.scala:93)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutions$$anonfun$play$api$db$evolutions$ApplicationEvolutions$$runEvolutions$1.apply$mcV$sp(ApplicationEvolutions.scala:53)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutions.withLock(ApplicationEvolutions.scala:100)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutions.play$api$db$evolutions$ApplicationEvolutions$$runEvolutions(ApplicationEvolutions.scala:49)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutions$$anonfun$start$1.apply(ApplicationEvolutions.scala:42)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutions$$anonfun$start$1.apply(ApplicationEvolutions.scala:42)
svcss_container |   at scala.collection.immutable.Stream.foreach(Stream.scala:594)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutions.start(ApplicationEvolutions.scala:42)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutions.<init>(ApplicationEvolutions.scala:151)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutionsProvider.get$lzycompute(EvolutionsModule.scala:49)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutionsProvider.get(EvolutionsModule.scala:49)
svcss_container |   at play.api.db.evolutions.ApplicationEvolutionsProvider.get(EvolutionsModule.scala:40)
svcss_container |   at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
svcss_container |   at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:72)
svcss_container |   at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:61)
svcss_container |   at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:62)
svcss_container |   at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
svcss_container |   at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1092)
svcss_container |   at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
svcss_container |   at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:194)
svcss_container |   at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
svcss_container |   at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:205)
svcss_container |   at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:199)
svcss_container |   at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1085)
svcss_container |   at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:199)
svcss_container |   at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:180)
svcss_container |   at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
svcss_container |   at com.google.inject.Guice.createInjector(Guice.java:99)
svcss_container |   at com.google.inject.Guice.createInjector(Guice.java:84)
svcss_container |   at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:185)
svcss_container |   at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:137)
svcss_container |   at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
svcss_container |   at play.core.server.ProdServerStart$.start(ProdServerStart.scala:51)
svcss_container |   at play.core.server.ProdServerStart$.main(ProdServerStart.scala:25)
svcss_container |   at play.core.server.ProdServerStart.main(ProdServerStart.scala)
svcss_container | Caused by: org.postgresql.util.PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
svcss_container |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:238)
svcss_container |   at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
svcss_container |   at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
svcss_container |   at org.postgresql.Driver.makeConnection(Driver.java:454)
svcss_container |   at org.postgresql.Driver.connect(Driver.java:256)
svcss_container |   at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:95)
svcss_container |   at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:101)
svcss_container |   at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:341)
svcss_container |   at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:193)
svcss_container |   at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:430)
svcss_container |   at com.zaxxer.hikari.pool.HikariPool.access$500(HikariPool.java:64)
svcss_container |   at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:570)
svcss_container |   at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:563)
svcss_container |   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
svcss_container |   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
svcss_container |   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
svcss_container |   at java.lang.Thread.run(Thread.java:748)
svcss_container | Caused by: java.net.ConnectException: Connection refused (Connection refused)
svcss_container |   at java.net.PlainSocketImpl.socketConnect(Native Method)
svcss_container |   at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
svcss_container |   at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
svcss_container |   at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
svcss_container |   at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
svcss_container |   at java.net.Socket.connect(Socket.java:589)
svcss_container |   at org.postgresql.core.PGStream.<init>(PGStream.java:69)
svcss_container |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:152)
svcss_container |   ... 16 more
svcss_container exited with code 255

这是它生成的错误日志。

我已经尝试了很多东西,比如检查项目中的Slick配置是否正确,但问题是 - 项目在开发和生产模式下运行在docker之外,为期1。5年。这是完全正确的配置。

我应该怎么可能会被错误行为在泊坞窗.ymlDockerfile

提问于
用户回答回答于

我认为Jack Gore有正确的想法:更新JDBC url以使用服务主机地址?

services:
    polymer:
        ...
        build: 
            context: ./svcss
            args:
                POSTGRES_JDBC_HOST: db <-- this gets you the host address
                ...

    db:
        container_name: pg_container
        restart: always
        image: postgres
        ...

然后更新数据库URL以使用主机环境arg,例如 "jdbc:postgresql://${POSTGRES_JDBC_HOST}/mydb_db?createDatabaseIfNotExist=true&useSSL=false"

扫码关注云+社区

领取腾讯云代金券