docker - Jenkins 多次构建 docker 端口冲突
问题描述
我有一个多模块 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>
问候
解决方案
您的配置通过以下行暴露给您的 Docker 主机系统(例如您的 Jenkins)的guest.port
端口8080
<port>guest.port:8080</port>
由于一个端口一次只能绑定到一个服务,以后的构建会发现无法绑定到该端口。
要解决这个问题,您可以为每个构建使用不同的端口,或者等待您想要使用的一个端口被另一个作业释放。
例如,您可以在执行之前将以下内容添加到您的 Jenkinsfile 中mvn
:
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
直到成功。
Ascript
是必要的,因为我们!=
对返回值进行了 caparison ( )。
最后netstat
返回当前绑定端口的列表,并且仅当端口是其中之一时grep
才会返回。0
8080
推荐阅读
- pandas - 将 pandas 数据框连接到 CSV 文件而不读取整个文件
- postgresql - 如何将计算保存为 PostgreSQL 表列中的默认值
- php - 如何理解 php 中的一个日期是否小于另一个减去一天?
- c# - 将值从表单标签传递到操作方法不起作用
- python - 无法安装 skbuild
- wordpress - 显示所有帖子并根据观看次数最多对它们进行排序
- error-handling - Gerrit 错误说无法检查权限
- spring-boot - 无法将两个 Neo4j 实例与 Spring boot/Spring 数据 neo4j 一起使用
- c# - 使用 ReactiveUI 禁用和启用文本字段更改上的按钮
- c++ - 如何检查 cmake 正在使用什么编译器?