Flyway无法与PostgreSQL ft. Docker和Jenkins连接

r8uurelv  于 2023-03-17  发布在  Jenkins
关注(0)|答案(1)|浏览(185)

在本文(https://faun.pub/how-to-install-docker-in-jenkins-container-4c49ba40b373)的帮助下,我在Docker中部署了Jenkins,因为它与主机的Docker sock连接,允许Docker中的Jenkins管理主机中的Docker。
现在,我已经将postgres部署为docker容器,并将5432暴露给主机。DB已经初始化并按预期工作。
在管道中,我用docker运行flyway,并使用conf和sql文件,这意味着jenkins会尝试在我的主机中将flyway作为docker容器运行。不,无论我在“flyway.url”中给予什么,它都不会连接到DB。

错误:无法连接到数据库。请配置url、用户和密码!
配置文件\路径.配置文件

flyway.url=jdbc:postgresql://192.168.29.2:5432/postgres
flyway.user=postgres
flyway.password=alpine98
flyway.connectRetries=60

Jenkins菲尔

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        stage('Temp'){
            steps {
                sh 'docker ps'
            }
        }
        stage('Version Check'){
            steps {
                sh 'docker run --rm flyway/flyway:9.15 version'
            }
        }
        stage('Migration') {
            steps {
                sh 'docker run --rm -v $WORKSPACE/db/migration:/flyway/sql -v $WORKSPACE/conf:/flyway/conf flyway/flyway:9.15 migrate'
            }
        }
        stage('Validation') {
            steps {
                sh 'docker run --rm -v $WORKSPACE/db/migration:/flyway/sql -v $WORKSPACE/conf:/flyway/conf flyway/flyway:9.15 validate'
            }
        }
        stage('Information') {
            steps {
                sh 'docker run --rm -v $WORKSPACE/db/migration:/flyway/sql -v $WORKSPACE/conf:/flyway/conf flyway/flyway:9.15 info'
            }
        }
    }
}

主机中运行的Docker容器列表:

  1. Jenkins -172.17.0.2-暴露端口8080和50000
  2. Postgres -172.17.0.3-暴露端口5432
  3. Flyway(将在jenkins作业运行时启动)
  • 由于所有设备都是在没有网络的情况下部署的,因此所有设备都Map在默认的“网桥”网络下 *
    Jenkins作业输出
+ docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED        STATUS        PORTS                                                                                                                             NAMES
52365808825e   portainer/portainer-ce:latest   "/portainer"             13 hours ago   Up 13 hours   0.0.0.0:8000->8000/tcp, :::8000->8000/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp, 0.0.0.0:9443->9443/tcp, :::9443->9443/tcp   portainer
ed6e91f86feb   gitea/gitea:1.18.5              "/usr/bin/entrypoint…"   13 hours ago   Up 13 hours   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp, 0.0.0.0:222->22/tcp, :::222->22/tcp                                                    gitea
fa32180a9a8c   postgres                        "docker-entrypoint.s…"   13 hours ago   Up 13 hours   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp                                                                                         postgres
1f169b2b6f9c   jenkins/jenkins:lts             "/usr/bin/tini -- /u…"   13 hours ago   Up 12 hours   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 0.0.0.0:50000->50000/tcp, :::50000->50000/tcp                                          jenkins
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Version Check)
[Pipeline] sh
+ docker run --rm flyway/flyway:9.15 version
Flyway Community Edition 9.15.2 by Redgate

Plugin Name           | Version | Licensed
--------------------- | ------- | --------
SqlFluffRulesEngine   | 1.2.1   | Licensed
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Migration)
[Pipeline] sh
+ docker run --rm -v /var/jenkins_home/workspace/ecp-flyway-integration_main/db/migration:/flyway/sql -v /var/jenkins_home/workspace/ecp-flyway-integration_main/conf:/flyway/conf flyway/flyway:9.15 migrate
ERROR: Unable to connect to the database. Configure the url, user and password!
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Validation)
Stage "Validation" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Information)
Stage "Information" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

请协助我解决这个问题。

wfauudbj

wfauudbj1#

默认网桥网络”“被视为Docker的旧版详细信息,不建议用于生产”“。您应该始终使用docker network create网络,然后使用docker run --net该网络上的容器。
您将需要删除并重新创建数据库容器。但是如果您使用

docker network create a_network
docker run -d \
  --net a_network \
  --name database \
  -v pgdata:/var/lib/postgresql/data \
  postgres:15

那么网络a_network上的任何容器都可以使用容器名database作为主机名。您永远不需要知道任何有关容器私有IP地址的信息。
通过此设置,您可以在Flyway配置中使用该主机名

flyway.url=jdbc:postgresql://database/postgres

当你实际运行容器时,你需要把它们放到网络上

sh 'docker run --net a_network --rm -v $WORKSPACE/db/migration:/flyway/sql -v $WORKSPACE/conf:/flyway/conf flyway/flyway:9.15 migrate'

这是Jenkins's Docker support稍有欠缺的地方:虽然几乎总是需要--net选项,但如果使用该路径运行容器,则需要在选项列表中显式提供该选项。

相关问题