为什么我需要主机网络在我的 jenkins 服务器上构建一个 docker 图像?

Why do I need the host network to build a docker image on my jenkins server?

在我的 Jenkins 服务器上(使用官方 jenkins image 部署),我需要 运行 下面的代码来构建我的 docker 镜像

def buildDockerImage() {
  sh "docker build --network=host -t $DOCKER_REPO ."
}

注意 --network=host 选项。

现在我的问题是,在我的开发笔记本电脑上,我不需要该选项来构建我的图像。这是为什么?为什么我的笔记本电脑上不需要它,但我需要在 jenkins 服务器上使用它? Jenkins 服务器托管在 Jelastic 环境中,如下所示:

env:
    topology:
        nodes:
          - image: jenkins/jenkins:lts
            count: 1
            cloudlets: 32
            nodeGroup: cp

Docker 是这样安装的:

mv /etc/init.d/kmod /etc/init.d/kmod.back
apt -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
apt update
apt -y install docker-ce docker-ce-cli containerd.io
service docker start
usermod -aG docker jenkins

如果没有该网络选项,我需要配置什么来实现它吗?

误会就在预装了Jenkins然后安装了Docker工具的地方。 Jelastic 提供使用变量和特殊重定向。默认情况下,Jenkins 从 80 端口重定向到 8080,您可以通过已经生成的环境名称直接访问它。 您应该遵循的方法是通过特殊的“JELASTIC_EXPOSE=DISABLED”变量禁用自动重定向并使用 public IP。如果您不喜欢使用 public IP,请查找端点功能。

ports 文章

中描述了所有相关内容

根据 Jelastic 的回答,我在安装我的 Jenkins CI/CD 环境的 jps 清单中添加了以下步骤:

actions:
    setupJenkinsForDocker:
      - api:
          - method: jelastic.environment.control.AddContainerEnvVars
            params:
              nodeGroup: cp
              vars:
                JELASTIC_EXPOSE: DISABLED
      - api:
          - method: jelastic.environment.control.AddEndpoint
            params:
              nodeId: ${nodes.cp.id}
              privatePort: 8080
              protocol: TCP 
              name: jenkins/jenkins

当然,这意味着我调整了所有在该 jenkins 环境中触发某些作业的 webhook,因为这需要在我的 jenkins 端点中有一个端口。

现在,当我构建需要互联网连接的 docker 图像时,我不再需要设置选项 --network=host