这是我的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年,处于开发和生产模式。这是完全正确的配置。
在docker .yml
或Dockerfile
中我可能会做错什么?
答案 0 :(得分:2)
我认为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"