spring - Gitlab CI 无法将 spring 应用程序连接到 postgres

标签 spring postgresql docker gitlab-ci

我在 gitlab yml 文件中有以下阶段。

    integration-tests:
      image: mydocker-hub-id/mvn-intergration-tests-image:latest
      stage: test
      services:
        - postgres
      script:
        - export PGPASSWORD=$POSTGRES_PASSWORD
        - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
        - cd source_code
        - pg_isready
        - mvn verify -P test-ci -DskipUTs=true
当为命令 pg_isready 执行作业时,我有响应 5432 没有响应。
当从每个集成测试执行测试时,我有错误:
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.
此阶段使用的图像基于 postgres 图像构建,该图像另外安装了 maven 和 java 11。
spring 应用程序无法与 postgres 服务器连接可能是什么问题。命令 postgres --version显示正确的版本。还命令
"psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
返回 OK 结果。
app.properties 中的 Spring db 连接如下:
spring.datasource.url=jdbc:postgresql://localhost:5432/test

最佳答案

我在 gitlab 文档中找到了有关 postgres 服务配置的详细信息

https://docs.gitlab.com/ee/ci/services/postgres.html?fbclid=IwAR1eDlOQ4ACn6zqw1auIetB07JRcCGi1Pjl-hpfGBM45ujvkImm6fHSSbgg
db url 应该包含主机 postgres 的名称而不是 localhost。

关于spring - Gitlab CI 无法将 spring 应用程序连接到 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63466025/

相关文章:

postgresql - Gitlab从7.11升级到8.0后无法启动postgresql、redis和sidekiq

google-chrome - 无法连接Chrome

java - Spring TransactionProxyFactoryBean 未加载 dao 服务

java - 我是否需要一个包含经常使用站点的 REST API 的 Java 应用程序的数据库?

postgresql - 当我在 PostgreSQL 中使用\d+ TABLE 命令时,输出换行

docker - 如何让 Docker 容器看到真实的用户 IP?

python-3.x - 如何从docker-py获取容器ID

spring - Spring Data JPA中@Modifying方法的返回值是什么意思

java - 我可以在 Spring MVC 的拦截器中使用 RedirectAttributes 或 FlashMap

postgresql - 如何获取存储在 PostgreSQL 中的 bytea 的 MIME 类型