gpt4 book ai didi

spring-boot - 当通过 Dockerfile 上的 gradle 图像触发时,Jooq 无法找到数据库

转载 作者:行者123 更新时间:2023-12-03 05:04:18 25 4
gpt4 key购买 nike

我有一个 Dockerfile,它使用 gradle 构建我的 Spring Boot 应用程序,然后将其复制到容器中并触发它。这是它的外观:

FROM gradle:5.4.1-jdk8-alpine AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build --no-daemon

FROM openjdk:8-jdk-alpine
EXPOSE 8080
RUN mkdir /app
COPY --from=build /home/gradle/src/build/libs/*.jar yurlapp.jar
ENTRYPOINT ["java","-jar" , "/yurlapp.jar"]

这很简单,它将执行 Gradle 并将 jar 添加到 docker 容器中。到现在为止还挺好。提供数据库后,此 Dockerfile 将在 docker-compose.yml 中使用(应用程序需要该数据库才能工作)。这就是 docker-compose.yml 的样子:
version: '2.1'
services:
yurldb:
image: postgres:11
container_name: yurldb
networks:
- yunet
ports:
- 5432:5432
environment:
- POSTGRES_DB=yurldb
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=somepassword
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/
- yudata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 10s
retries: 5
flyway:
image: boxfuse/flyway
command: -url=jdbc:postgresql://yurldb:5432/yurldb -schemas=public -user=postgres -password=somepassword migrate
networks:
- yunet
volumes:
- .:/flyway/sql
depends_on:
yurldb:
condition: service_healthy
yurlapp:
container_name: yurlapp
image: javing/yurlapp:0.0.1
build:
context: .
dockerfile: Dockerfile
networks:
- yunet
ports:
- 8080:8080
environment:
- DB_HOST=yurldb #the name of the DB container is used as url
depends_on:
yurldb:
condition: service_healthy
flyway:
condition: service_healthy
networks:
yunet:
volumes:
yudata:

如您所见,应用程序镜像是最后构建的,一旦数据库镜像准备好并且在 flyway 执行迁移之后。我在构建应用程序期间发生的问题。
因此,一旦 gradle 启动,它将完成它必须做的所有事情。但在某一时刻,它需要 Jooq,根据数据库上的现有表自动生成一些类。在这里出了点问题。构建爆炸并告诉我:

Task :generateSampleJooqSchemaSource FAILED


FAILURE: Build failed with an exception.

* Where:
Build file '/home/gradle/src/build.gradle' line: 81

* What went wrong:
Execution failed for task ':generateSampleJooqSchemaSource'.
> jOOQ source code generation failed:

Jun 01, 2020 7:37:39 PM org.jooq.tools.JooqLogger info
INFO: Initialising properties : /home/gradle/src/build/tmp/generateSampleJooqSchemaSource/config.xml
Jun 01, 2020 7:37:39 PM org.jooq.tools.JooqLogger error
SEVERE: Cannot read /home/gradle/src/build/tmp/generateSampleJooqSchemaSource/config.xml. Error : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
at org.postgresql.Driver.makeConnection(Driver.java:459)
at org.postgresql.Driver.connect(Driver.java:261)
at org.jooq.codegen.GenerationTool.run0(GenerationTool.java:342)
at org.jooq.codegen.GenerationTool.run(GenerationTool.java:221)
at org.jooq.codegen.GenerationTool.generate(GenerationTool.java:216)
at org.jooq.codegen.GenerationTool.main(GenerationTool.java:188)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.postgresql.core.PGStream.<init>(PGStream.java:81)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:93)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
... 8 more

它说它无法连接到数据库。这是为什么?这是我在 build.gradle 文件中的 Jooq 配置
jooq {
sample(sourceSets.main) {
jdbc {
driver = 'org.postgresql.Driver'
url = 'jdbc:postgresql://localhost:5432/yurldb'
user = 'postgres'
password = 'somepassword'
}
generator {
database() {
name = 'org.jooq.meta.postgres.PostgresDatabase'
inputSchema = 'public'
includes = '.*'
}
target {
packageName = 'com.javing.yurl'
directory = 'build/generated/java'
}
}
}
}

tasks.generateSampleJooqSchemaSource.with {
def out = new ByteArrayOutputStream()
javaExecSpec = { JavaExecSpec s ->
s.standardOutput = out
s.errorOutput = out
s.ignoreExitValue = true
s.jvmArgs '-Xmx512M'
}
execResultHandler = { ExecResult r ->
if (r.exitValue != 0) {
throw new RuntimeException('jOOQ source code generation failed:\n\n' + out.toString())
}
}
}

我很困惑,我不确定我是否理解发生了什么。如果我要使用 docker-compose 仅​​自行启动数据库镜像,然后从终端手动运行 gradle(而不是来自 docker 的 gradle),那么这些类将毫无问题地生成。所以我不知道何时使用 Dockerfile Jooq 中的 gradle 无法找到数据库。任何的想法?
我是否还需要以某种方式从 Dockerfile 触发 Jooq,就像我触发 gradle 一样?有人可以帮我解决这个问题,我有点卡住了。

更新
我尝试将 jooq 配置中的 url 更改为使用 yurldb 而不是 localhost 因为可能在容器内联网时,url resloves 到容器的名称,但这不起作用。会发生什么:
FAILURE: Build failed with an exception.

* Where:
Build file '/home/gradle/src/build.gradle' line: 81

* What went wrong:
Execution failed for task ':generateSampleJooqSchemaSource'.
> jOOQ source code generation failed:

Jun 01, 2020 8:29:57 PM org.jooq.tools.JooqLogger info
INFO: Initialising properties : /home/gradle/src/build/tmp/generateSampleJooqSchemaSource/config.xml
Jun 01, 2020 8:30:08 PM org.jooq.tools.JooqLogger error
SEVERE: Cannot read /home/gradle/src/build/tmp/generateSampleJooqSchemaSource/config.xml. Error : The connection attempt failed.
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:297)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
at org.postgresql.Driver.makeConnection(Driver.java:459)
at org.postgresql.Driver.connect(Driver.java:261)
at org.jooq.codegen.GenerationTool.run0(GenerationTool.java:342)
at org.jooq.codegen.GenerationTool.run(GenerationTool.java:221)
at org.jooq.codegen.GenerationTool.generate(GenerationTool.java:216)
at org.jooq.codegen.GenerationTool.main(GenerationTool.java:188)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.postgresql.core.PGStream.<init>(PGStream.java:81)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:93)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
... 8 more

最佳答案

问题是你有 localhost作为 jooq 配置中的数据库主机build.gradle 中的插件.

您已正确设置 DB_HOST 的环境变量在其他地方,所以我假设您了解服务名称是 DNS 名称,当它们连接到公共(public)网络时,您可以使用它来引用其他服务。你应该改变

url = 'jdbc:postgresql://localhost:5432/yurldb'


url = 'jdbc:postgresql://yurldb:5432/yurldb'

或者您可以在应用服务的环境变量中获取 db 的主机,然后从 build.gradle 中读取它:
def db = System.getenv('DB_HOST')
url = "jdbc:postgresql://${db}:5432/yurldb"

关于spring-boot - 当通过 Dockerfile 上的 gradle 图像触发时,Jooq 无法找到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62140023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com