【发布时间】:2021-02-05 17:00:27
【问题描述】:
我的 DevOps 环境有以下设置,使用 Docker Compose 进行编排:
(简体)docker-compose.yml:
version: '3.7'
services:
nexus:
build: ./nexus/.
expose:
- 8081
networks:
- devops-network
jenkins:
build: ./jenkins/.
expose:
- 8080
depends_on:
- nexus
networks:
- devops-network
nginx:
image: nginx:1.19.5
ports:
- 80:80
depends_on:
- nexus
- jenkins
networks:
- devops-network
networks:
devops-network:
(简体)nginx.conf:
http {
upstream docker-jenkins {
server jenkins:8080;
}
upstream docker-nexus {
server nexus:8081;
}
server {
server_name jenkins.homenetwork.dns;
location / {
proxy_pass http://docker-jenkins;
}
}
server {
server_name nexus.homenetwork.dns;
location / {
proxy_pass http://docker-nexus;
}
}
}
Jenkins 可以通过 Docker 网络与 Nexus 通信。如果我进入 Jenkins 容器,那么 ping nexus 和 curl http://nexus:8081 会给我积极的回应(我从 Nexus 获得反馈)。
但是当我使用 Maven 的嵌套 Docker 代理运行 Jenkins 管道时:
pipeline {
agent {
docker { // successfully pulls image
image 'maven:3.6.3-openjdk-11'
args '-v /root/.m2:/root/.m2'
}
}
stages {
stage('Maven build') {
steps {
mavenBuildStep()
}
}
stage('Upload'){
steps {
withMaven(mavenSettingsConfig: 'cdb64ca9-d8e1-4d19-b486-e86c0ee75f50')
{
echo 'uploading Maven artifacts'
sh 'mvn deploy -DskipTests' // fails because it can't find nexus
}
}
}
}
}
和具有上述 id 的 maven settings.xml:
<servers>
<server>
<id>nexus-snapshots</id>
<username>jenkins</username>
<password>${JENKINS_NEXUS_PASSWORD}</password>
</server>
</servers>
这在我的pom.xml:
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://nexus:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
然后它说:
[错误] 无法在项目父项上执行目标 org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy):无法检索远程元数据 ... 从/到 nexus -snapshots (http://nexus:8081/repository/maven-snapshots/):http://nexus:8081/repository/maven-snapshots/.../1.0.0-SNAPSHOT/maven-metadata 传输失败。 xml:未知主机关系:没有与主机名关联的地址
当我将pom.xml 中的存储库 url 更改为 Nexus 容器的网络 IP 时:
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://172.26.0.2:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
然后超时:
[ERROR] 无法在项目父级上执行目标 org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy):...连接到 172.26.0.2:8081 [/ 172.26.0.2] 失败:连接超时
发生了什么事?我使用setfacl -m user:jenkins:rw /var/run/docker.sock 将主机 Docker 守护程序映射到 Jenkins 容器,所以它应该能够访问 Nexus 容器吗?当我在管道中拉取 Docker 映像时,该映像会添加到我的主机系统中,并且下次不需要再次下载,因此这似乎处于正常工作状态。是不是因为“Docker inside Docker”无法访问devop-network?
理想情况下,我可以在我的 pom 中将http://nexus.homenetwork.dns/repository/maven-snapshots/ 设置为 url,但这似乎是不可能的。有没有人有这种设置的经验?
更新
为了验证我的怀疑,我在 Jenkins 管道中使用了 agent any 而不是 Docker 代理,并在 Jenkins 容器中安装了 maven。使用该设置,运行 Jenkins 管道会正确上传带有 url http://nexus:8081/repository/maven-snapshots/ 的快照。
这不是一个糟糕的解决方案,但出于好奇,我想知道如何让它与 Docker 代理一起工作。
【问题讨论】:
-
我的第一个问题是:为什么你的 nexus 存储库管理器前面有代理? nginx 服务于什么目的?
-
@khmarbaise 漂亮的网址是您问题的有效答案吗?我第一次尝试建立一个devops环境,所以我可能在做多余/奇怪的事情。当我在台式机上进行开发时,这些容器托管在我的笔记本电脑上。我想转到一个可读的 url 并让它从我的笔记本电脑访问应用程序。如果我不使用代理,我需要将应用程序托管在笔记本电脑本地主机的不同端口上。使用 nginx 一切都在 localhost:80 下。
标签: maven jenkins docker-compose