简体   繁体   English

Docker 推入 Jenkins - 拒绝:请求的资源访问被拒绝

[英]Docker push in Jenkins - denied: requested access to the resource is denied

So I'm trying to set up a pipeline in Jenkins to build image and push them to Docker hub.所以我试图在 Jenkins 中建立一个管道来构建图像并将它们推送到 Docker 集线器。 My credentials in Manage 'Jenkins' are called the same as "docker-hub-credentials" and seem to be used.我在 Manage 'Jenkins' 中的凭据被称为与“docker-hub-credentials”相同的名称,并且似乎已被使用。

It can build, but it just doesn't get through the push... Help?它可以构建,但它只是无法通过推动...帮助? I've been on that for hours and I'm not sure what I' missing.我已经做了几个小时了,我不确定我错过了什么。

I've already tried using docker login, but jenkins doesn't allow it.我已经尝试过使用 docker 登录,但 jenkins 不允许。

stage('Build image') {
    /* This builds the actual image; synonymous to
     * docker build on the command line */

     bat 'docker build -t username/foldername:build . '    }


stage('Push image') {
    /* Finally, we'll push the image with two tags:
    docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {
        bat 'docker push username/foldername:build'
    }
}

I expect the image to be pushed, but I have this instead:我希望图像被推送,但我有这个:

The push refers to repository [docker.io/username/foldername]
a73d7d9f4346: Preparing
964bdfb24a54: Preparing
1af124d739c9: Preparing
6cffeea81e5d: Preparing
614a79865f6d: Preparing
612d27bb923f: Preparing
ef68f6734aa4: Preparing
612d27bb923f: Waiting
ef68f6734aa4: Waiting
denied: requested access to the resource is denied

I found the answer!!!我找到了答案!!!

 stage('Push image') {
        withDockerRegistry([ credentialsId: "docker-hub-credentials", url: "" ]) {
        bat "docker push devopsglobalmedia/teamcitydocker:build"
        }

In Image push stage, you can do docker login first and then push the image.Image push阶段,您可以先进行docker login ,然后再推送镜像。 Try the following for docker login:尝试以下 docker 登录:

stage('Push image') {
    withCredentials([usernamePassword( credentialsId: 'docker-hub-credentials', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
        def registry_url = "registry.hub.docker.com/"
        bat "docker login -u $USER -p $PASSWORD ${registry_url}"
        docker.withRegistry("http://${registry_url}", "docker-hub-credentials") {
            // Push your image now
            bat "docker push username/foldername:build"
        }
    }
}

Make sure the registry url is correct.确保注册表 URL 正确。 withCredentials([usernamePassword(...)]) method above will set two environment variables USER and PASSWORD which are your docker registry credentials from credentials id docker-hub-credentials . withCredentials([usernamePassword(...)])方法将设置两个环境变量USERPASSWORD ,它们是来自凭证 ID docker-hub-credentials docker 注册表docker-hub-credentials

A better option is to use Docker pipeline plugin (it comes in the recommended plugins).更好的选择是使用Docker 管道插件(它包含在推荐的插件中)。

node {

  checkout scm
  def dockerImage

  stage('Build image') {
    dockerImage = docker.build("username/repository:tag")
  }

  stage('Push image') {
    dockerImage.push()
  }   

}

Doing it this way, you must specify the credentials of the docker registry in the Pipeline Model Definition .这样做,您必须在Pipeline Model Definition 中指定 docker 注册表的凭据。

Docker pipeline plugin has problems applying the credentials assigned in Pipeline Model Definition to projects with Multi-branch pipeline. Docker 管道插件在将管道模型定义中分配的凭据应用于具有多分支管道的项目时存在问题。 That is, if using the above code you continue to receive the error:也就是说,如果使用上面的代码,您会继续收到错误消息:

denied: requested access to the resource is denied拒绝:请求访问资源被拒绝

Then you must specify the credentials in the Jenkinsfile as follows:然后您必须在 Jenkinsfile 中指定凭据,如下所示:

node {

  checkout scm
  def dockerImage

  stage('Build image') {
    dockerImage = docker.build("username/repository:tag")
  }

  stage('Push image') {
    docker.withRegistry('https://registry-1.docker.io/v2/', 'docker-hub-credentials') {
      dockerImage.push()
    }
  }

}

You can modify the URL to a custom registry if you need it如果需要,您可以将 URL 修改为自定义注册表

I had the same issue recently, it was a wrong the docker registry url我最近遇到了同样的问题,docker registry url 是错误的

The issue - https://index.docker.io/v1问题 - https://index.docker.io/v1

The fix - https://index.docker.io/v1/修复 - https://index.docker.io/v1/

Yep, it was an issue.是的,这是一个问题。 I was not able to push it to docker registry.我无法将其推送到 docker 注册表。

I have spent like half days to solve it.我花了半天时间来解决它。 Please make sure Docker Pipeline Plugin installed.请确保已安装Docker Pipeline Plugin

script {
  withDockerRegistry([ credentialsId: "Docker-Hub-Cred", url: "https://index.docker.io/v1/" ]){            
                             
  }
}  

If you want to push private repository then below commands you have to follow:如果要推送私有存储库,则必须遵循以下命令:

1st: you have to make sure dockerhub credentials on jenkins.第一:您必须确保 jenkins 上的 dockerhub 凭据。 2nd: You have creat a job with pipeline project 3rd: Then you have to push your project with jenkinsfile.第二:你已经创建了一个管道项目的工作 第三:然后你必须用 jenkinsfile 推送你的项目。 4th: now you can build jenkins.第四:现在你可以构建詹金斯了。

I am giving full Jenkinsfile it will help to solve this permission denied problem and successfully u can create docker image and push to dockerhub.我正在提供完整的 Jenkinsfile,它将有助于解决此权限被拒绝的问题,并且您可以成功创建 docker 映像并推送到 dockerhub。

 pipeline { agent any stages{ stage('Maven Install'){ agent any steps{ bat 'mvn -f pom.xml clean install' } } stage('Docker Build'){ agent any steps{ bat 'docker build -t dockerhub_username/repository_name/docker-jenkins-integration .' } } stage('Push image'){ agent any steps{ withDockerRegistry([ credentialsId: "id", url: "" ]) { bat "docker tag dockerhub_username/repository_name/docker-jenkins-integration dockerhub_username/repository_name:docker-jenkins-integration" bat "docker push dockerhub_username/repository_name:docker-jenkins-integration" } } } } }

As of today, Docker pipeline plugin gives groovy methods to login to docker hub, build image and finally push it.截至今天,Docker 管道插件提供了登录 docker hub、构建镜像并最终推送的 groovy 方法。 Below is a sample code for a declarative pipeline以下是声明性管道的示例代码

pipeline {
agent any
stages {
    stage('Push container') {
        steps {
            echo "Workspace is $WORKSPACE"
            dir("$WORKSPACE/dir-to-Dockerfile") {
                script {
                    def REGISTRY_URL = "https://index.docker.io/v1/"
                    def USER_NAME = "your-registry-username"
                    docker.withRegistry("$REGISTRY_URL", 'DockerHub-Cred-ID-Defined-In-Jenkins') {
                        def image = docker.build("$USER_NAME/some-image-name:${env.BUILD_ID}")
                        image.push()
                    }
                }
            }
        }
    }
}
}

I solved by add:我通过添加解决了:

stage('Push Docker Image') {
            steps {
                withDockerRegistry([credentialsId: "docker_auth", url: "https://index.docker.io/v1/"]) {
                    bat "docker push IMAGE_NAME:latest"
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker 推“拒绝:请求访问资源被拒绝” - Docker push "denied: requested access to the resource is denied" Docker 推送到私有注册表错误 [拒绝:请求访问资源被拒绝] - Docker push to private registry error [denied: requested access to the resource is denied] docker 推送错误“被拒绝:请求访问资源被拒绝” - docker push error "denied: requested access to the resource is denied" 无法将Docker映像推送到gitlab:拒绝:请求的对资源的访问被拒绝 - Impossible to push docker images to gitlab: denied: requested access to the resource is denied 无法将 Docker 镜像推送到 Docker Hub; 请求访问资源被拒绝 - Cannot push Docker images to Docker Hub; requested access to the resource is denied Docker请求访问资源被拒绝 - Docker requested access to the resource is denied Docker被拒绝:请求的对资源的访问被拒绝 - Docker denied: requested access to the resource is denied 拒绝:请求访问资源被拒绝 - Docker - denied: requested access to the resource is denied - Docker 拒绝:请求访问资源被拒绝:docker - denied: requested access to the resource is denied: docker Docker Push在Red Hat Server中失败,错误为“ **拒绝:请求对资源的访问被拒绝**” - Docker Push has failed in Red Hat Server with error “**denied: requested access to the resource is denied**”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM