gpt4 book ai didi

docker - Jenkins 多次构建 docker 端口冲突

转载 作者:行者123 更新时间:2023-12-02 19:30:02 26 4
gpt4 key购买 nike

我有一个多模块 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>

问候

最佳答案

您的配置暴露了 guest.port到港口 8080您的 Docker 主机系统(例如您的 Jenkins)与以下行

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

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

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

例如,您可以在执行 mvn 之前将以下内容添加到 Jenkinsfile :
timeout(time: 10, unit: "MINUTES") {
waitUntil {
script {
sh(script: 'netstat -lnpt 2>&1 | grep ":8080"', returnStatus: true) != 0
}
}
}
sh "mvn ..."
timeout step 导致 Jenkins 在 10 分钟后取消。
waitUntil步骤使 Jenkins 重试 script直到成功。

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

最后 netstat返回当前绑定(bind)的端口列表和 grep将返回 0仅当端口 8080就是其中之一。

关于docker - Jenkins 多次构建 docker 端口冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52382776/

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