【问题标题】:Jenkins multiple builds docker port conflictsJenkins 多次构建 docker 端口冲突
【发布时间】:2018-09-18 22:06:02
【问题描述】:

我有一个多模块 Maven 项目,我在同时进行多个 Jenkins 构建时遇到了 docker 端口冲突。

我在 pom.xml 文件中使用 docker-maven-plugin

我该如何解决这个问题?

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>start</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>stop</goal>
                <goal>build</goal>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <images>
            <image>
                <name>guest/guest-main:${project.version}</name>
                <alias>guest</alias>
                <run>
                    <env>
                        <myapp_ENDPOINT>http://mock:8081/mycalcService</myapp_ENDPOINT>
                    </env>
                    <namingStrategy>alias</namingStrategy>
                    <dependsOn>
                        <container>mock</container>
                    </dependsOn>
                    <links>
                        <link>mock:mock</link>
                    </links>
                    <ports>
                        <port>guest.port:8080</port>
                    </ports>
                    <wait>
                        <log>Started guestServiceApplication</log>
                        <time>60000</time>
                    </wait>
                </run>
            </image>
            <image>
                <alias>mock</alias>
                <name>guest/myapp-mock:${project.version}</name>
            </image>
        </images>
    </configuration>
</plugin>

问候

【问题讨论】:

  • 为什么需要在构建时公开端口?
  • 我对此很陌生 - 请解释一下
  • 你在使用 Jenkinsfile 吗?

标签: docker jenkins


【解决方案1】:

您的配置将 guest.port 暴露给您的 Docker 主机系统(例如您的 Jenkins)的端口 8080,使用以下行

<port>guest.port:8080</port>

由于一个端口一次只能绑定到一个服务,以后的构建会发现无法绑定到该端口。

要解决这个问题,您可以为每个构建使用不同的端口,或者等待您想要使用的一个端口被另一个作业释放。

例如,您可以在执行 mvn 之前将以下内容添加到您的 Jenkinsfile 中:

timeout(time: 10, unit: "MINUTES") {
    waitUntil {
        script {
            sh(script: 'netstat -lnpt 2>&1 | grep ":8080"', returnStatus: true) != 0
        }
    }
}
sh "mvn ..."

timeout 步骤导致 Jenkins 在 10 分钟后取消。

waitUntil 步骤使 Jenkins 重试script,直到成功。

script 是必要的,因为我们对返回值进行了修饰 (!=)。

最后,netstat 返回当前绑定端口的列表,grep 仅当端口 8080 是其中之一时才会返回 0

【讨论】:

    猜你喜欢
    • 2021-07-23
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2022-06-25
    • 2021-01-04
    • 2020-10-18
    • 2016-07-25
    相关资源
    最近更新 更多