简体   繁体   中英

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. My credentials in Manage 'Jenkins' are called the same as "docker-hub-credentials" and seem to be used.

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.

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. Try the following for docker login:

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. withCredentials([usernamePassword(...)]) method above will set two environment variables USER and PASSWORD which are your docker registry credentials from credentials id docker-hub-credentials .

A better option is to use Docker pipeline plugin (it comes in the recommended plugins).

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 .

Docker pipeline plugin has problems applying the credentials assigned in Pipeline Model Definition to projects with Multi-branch pipeline. 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:

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

I had the same issue recently, it was a wrong the docker registry url

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

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

Yep, it was an issue. I was not able to push it to docker registry.

I have spent like half days to solve it. Please make sure Docker Pipeline Plugin installed.

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. 2nd: You have creat a job with pipeline project 3rd: Then you have to push your project with 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.

 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. 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"
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM