docker 登录 Jenkins 管道无法从 docker 中心拉取 public 图像

Jenkins pipeline fails for docker login to pull public image from dockerhub

使用下面的声明性管道代码,我试图从 jenkins 的 dockerhub 中提取 public 图像,但是它失败并出现以下错误。

pipeline {  
    agent {
        docker {
            image 'ubuntu:latest'
            label "jenkins-slave-01"
            }
    }
    stages {    
        stage('Build') {    
            steps { 
                sh 'cat /etc/lsb-release'
            }   
        }   
        stage('Deploy') {   
            steps { 
                sh 'cat /etc/lsb-release'
            }   
        }           
    }   
}

Jenkins 控制台输出:-

[Pipeline] withDockerRegistry
Using the existing docker config file.Removing blacklisted property: auths$ docker login -u test -p ******** https://index.docker.io/v1/
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: docker login failed
Finished: FAILURE

即使从 dockerhub 中提取 public 图像也需要在 jenkins 中进行身份验证?因为我已经在 Declarative Pipeline (Docker) 部分的 jenkins 中配置了私有注册表(nexus),所以我想从 dockerhub 中提取 public 图像。

根据我们对您最初问题的评论的交流,我们可以了解到您在“声明性管道”插件上的 docker 配置指向拉通缓存配置,而不是public docker 注册表,您可以在此处 https://docs.docker.com/registry/recipes/mirror/.

查看相关信息

如果您想使用在插件上配置的那个,您应该能够配置凭据以访问该存储库,然后 select 这些凭据将用于此私有存储库,并为即将到来的人提供上下文要查看此问题,您在命令中提到的插件具有以下配置:

关于注册表凭据,您有 selected 的东西吗? 在我的例子中,我使用 artifactory 存储库,但没有使用此插件配置它,而是在 Jenkins 上配置了凭据,并在我的管道上声明代理时调用完整存储库 URL。

也许您可以避免使用在管道上配置的管道,说明完整的 docker URL 用于下载 ubuntu 而不是仅使用容器+版本,例如:

agent {
    docker {
        image 'registry.hub.docker.com/library/ubuntu:latest'
        label "jenkins-slave-01"
        }
}

这仍然存在问题,没有好的解决方法,但这是一个丑陋的 hack。如果你可以在主机上使用 docker cli,你就可以绕过 Jenkins 的 docker 客户端。

注意:这假设您有 linux 个执行您的 docker 容器的主机。如果您有不同类型的设置,这可能不适合您。

先手动拉取镜像到主机上

pipeline {  
    agent any // Get an agent.  You may want to define a label here.
    stages {    
        stage('pull') {    
            steps { 
                sh 'docker pull ubuntu:latest' // Pull the image down to the agent.
            }   
        }   
        stage('docker') { // Nested stages all run on the docker agent
            agent {
                docker {
                    image "ubuntu:latest" // It will find the image locally
                    reuseNode true        // important!
                }
            }
            stages {
                stage('build') {
                    steps { 
                        sh 'cat /etc/lsb-release'
                    }
                }
                stage('deploy') {
                    steps { 
                        sh 'cat /etc/lsb-release'
                    }
                }
            }
        }           
    }   
}