无法使用Jenkins的kubernetes-plugin构建docker映像

时间:2019-09-15 11:45:15

标签: docker jenkins kubernetes

我正在使用kubernetes-plugin,但在K8S容器顶部构建docker映像时遇到了一些问题:

我正在使用以下方法创建POD:

podTemplate(containers: [
    containerTemplate(
        name: 'docker-build',
        image: 'docker',
        command: 'cat',
        ttyEnabled: true
    )
],
volumes: [
    hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
])
{
    node(POD_LABEL) {
        stage("Checkout") {
            dir("${env.WORKSPACE}/code") {
                script {
                    // Checkout - Works
                }
            }
        }

        stage ('Build docker images') {
            container('docker-build') {
                dir("${env.WORKSPACE}/code") {
                    sh """
                    ./build-images
                    """
                }
            }
        }
    }
}

但是在docker构建步骤中失败:

Err:1 http://deb.debian.org/debian stretch InRelease
  Temporary failure resolving 'deb.debian.org'
Err:2 http://security.debian.org/debian-security stretch/updates InRelease
  Temporary failure resolving 'security.debian.org'
Err:3 http://deb.debian.org/debian stretch-updates InRelease
  Temporary failure resolving 'deb.debian.org'
Reading package lists...
[91mW: Failed to fetch http://deb.debian.org/debian/dists/stretch/InRelease  Temporary failure resolving 'deb.debian.org'
W: Failed to fetch http://security.debian.org/debian-security/dists/stretch/updates/InRelease  Temporary failure resolving 'security.debian.org'
W: Failed to fetch http://deb.debian.org/debian/dists/stretch-updates/InRelease  Temporary failure resolving 'deb.debian.org'
W: Some index files failed to download. They have been ignored, or old ones used instead.
[0mReading package lists...
Building dependency tree...
Reading state information...
[91mE: Unable to locate package libpam-dev
E: Unable to locate package libpcap0.8-dev
E: Couldn't find any package by glob 'libpcap0.8-dev'
E: Couldn't find any package by regex 'libpcap0.8-dev'
E: Unable to locate package libpq5[0m[91m
E: Unable to locate package libtins-dev
E: Unable to locate package openjdk-8-jdk-headless
E: Unable to locate package python3

使用kubectl exec访问POD并尝试构建时,它因相同的错误而失败:

docker build -t my_test .

当尝试使用--network=host构建相同的文件时有效

docker build --network=host -t my_test .

我正在尝试理解为什么需要--network=host才能正常工作。

顺便说一句-当我在Jenkins奴隶吊舱中并且尝试下载任何软件包或访问Internet时,一切都很好,只有当我尝试构建Docker映像并尝试下载时,它才会发生在此过程中打包。

我怀疑docker build因某些网络配置错误而失败,或者在此docker构建于docker之上的过程中docker网络处于错误状态。

到目前为止,我已经尝试过:

  • 使用hostNetwork: true创建广告连播,但没有帮助。
  • 使用privileged: true创建广告连播,但没有帮助。
  • 许多其他黑客使Pod在不同的网络上运行,但效果也不佳。

请协助。

1 个答案:

答案 0 :(得分:2)

默认情况下,最新版本的经AWS EKS优化的AMI禁用docker bridge网络。要启用它,请将bootstrap_extra_args参数添加到您的工作组模板中。 Source