无法在我的Jenkins管道中链接两个“ sidecar”容器

时间:2019-04-16 17:53:31

标签: mysql unit-testing docker jenkins-pipeline

当前,我正在访问一个外部数据库以在管道中执行单元测试。我想通过在管道中实施本地测试来增加构建的运行时间。

我正在尝试使用this格式。

docker.image('mysql:5.6').withRun(' -e "MYSQL_ROOT_PASSWORD=pass" ') { c ->

    sh('docker ps -l')

    docker.image('mysql:5.6').inside(" --link ${c.id}:db ") {

        /* Wait until mysql service is up */
        sh ("while ! mysqladmin ping -hdb:3306 -ppass; do sleep 1; done")

    }

    docker.image(imageName).inside(" --link ${c.id}:db ") {
        /*
        * Run some tests which require MySQL, and assume that it is
        * available on the host name `db`
        */
        sh("echo test")
        //run some tests        
    }
}

代码未到达第二张图片。由于can't connect to the host 'db',它之前会出错。输出如下:

+ docker run -d -e MYSQL_ROOT_PASSWORD=pass mysql:5.6
[Pipeline] dockerFingerprintRun
[Pipeline] sh
+ docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED                  STATUS                  PORTS               NAMES
63c18d0dfbd6        mysql:5.6           "docker-entrypoint..."   Less than a second ago   Up Less than a second   3306/tcp            sleepy_haibt2
[Pipeline] sh
+ docker inspect -f . mysql:5.6
.
[Pipeline] withDockerContainer
slave-37188a47f08d1 seems to be running inside container 5e34252...
[Pipeline] {
[Pipeline] sh
+ mysqladmin ping -hdb:3306 -ppass
Warning: Using a password on the command line interface can be insecure.
mysqladmin: connect to server at 'db:3306' failed
error: 'Unknown MySQL server host 'db:3306' (2)'
Check that mysqld is running on db:3306 and that the port is 3306.
You can check this by doing 'telnet db:3306 3306'
+ sleep 1
$ docker run -t -d -u 0:0 --link 63c18d0d...:db -w /home/jenkins/workspace/-database-for-testing-in-jenkins --volumes-from 5e34252... -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** mysql:5.6 cat
$ docker top d003b0649... -eo pid,comm
+ mysqladmin ping -hdb:3306 -ppass
Warning: Using a password on the command line interface can be insecure.
mysqladmin: connect to server at 'db:3306' failed
error: 'Unknown MySQL server host 'db:3306' (2)'
Check that mysqld is running on db:3306 and that the port is 3306.
You can check this by doing 'telnet db:3306 3306'
+ sleep 1

我尝试从:3306删除mysqladmin

我尝试将-p 3306:3306添加到主容器和sidecar mysql容器中。

我尝试将-u 0添加到两个容器中。

Docker Version 18.09

Jenkins Version 2.164

K8s Version 1.11

不确定下一步该怎么做。有建议吗?

1 个答案:

答案 0 :(得分:0)

不确定这里出了什么问题,但是最后使用withRun作为第一辆小车,并使用docker exec进行ping操作。这样就解决了。

相关问题