Docker Community Forums

Share and learn in the Docker community.

Error when trying to push an image from Jenkins to Docker Hub

I am getting an error when trying to push an image from Jenkins to the docker hub.

denied: requested access to the resource is denied

This is the code that I think is problematic:

        stage('Build') {
            steps {
                //  Building new image
                sh 'docker image build -t $DOCKER_HUB_REPO:latest .'
                sh 'docker image tag $DOCKER_HUB_REPO:latest $DOCKER_HUB_REPO:$BUILD_NUMBER'

                //  Pushing Image to Repository
                sh 'docker push efras/flask-cicd-pipeline:$BUILD_NUMBER'
                sh 'docker push efras/flask-cicd-pipeline:latest'

                echo "Image built and pushed to repository"
            }
        }

I’ve changed the code to add docker.withregistry method but it’s still erroring out without any useful error message:

        stage('Build') {
            steps {
                script {
                    docker.withRegistry('https://registry.hub.docker.com', 'docker_hub_login') {
                        //  Building new image
                        sh 'docker image build -t $DOCKER_HUB_REPO:latest .'
                        sh 'docker image tag $DOCKER_HUB_REPO:latest $DOCKER_HUB_REPO:$BUILD_NUMBER'

                        //  Pushing Image to Repository
                        sh 'docker push efras/flask-cicd-pipeline:$BUILD_NUMBER'
                        sh 'docker push efras/flask-cicd-pipeline:latest'

                        echo "Image built and pushed to repository"
                    }
                }
            }
        }

docker_hub_login is my docker hub credential in Jenkins.

Could someone please assist me in what I should try next?

You can check my Jenkinsfile here:
flask-docker-app-jenkins-pipeline/Jenkinsfile at master · Hexalogy/flask-docker-app-jenkins-pipeline (github.com)

EDIT: I’m following this tutorial

If it says your access is denied, then your credentials are probably wrong. Since I don’t really know the answer, I am just guessing, but are you sure you can use the credentials from docker.withRegistry() when you run the build using a shell command? The documentation uses a different approach:

node {
    checkout scm

    docker.withRegistry('https://registry.example.com', 'credentials-id') {

        def customImage = docker.build("my-image:${env.BUILD_ID}")

        /* Push the container to the custom Registry */
        customImage.push()
    }
}

@rimelek the example shows a scripted pipeline, while @efras uses a declarative pipeline - but instead of fully levaraging plugins, just uses a plugin for the registry auth and does everything else in sh commands.

The docker.withRegisty() block of @rimelek’s example, could be used to replace the docker.withRegistry() block of @efras post as it’s wrapped in a script block, which allows to mix declarative and scripted pipeline syntax. It’s worth trying…

2 Likes

i’ve tried the solution mentioned but still no luck :confused:

stage('Build') {
            steps {
                script {
                    docker.withRegistry('https://registry.hub.docker.com', 'docker_hub_login') {
                        def customImage = docker.build("$DOCKER_HUB_REPO:${env.BUILD_ID}")

                        /* Push the container to the custom Registry */
                        customImage.push()
                    }
                }
            }
        }

It’s still giving an empty error in Jenkins which is annoying because it’s not really helpful. I wonder if there is an equivalent command for docker.withRegistry('https://registry.example.com', 'credentials-id') in a declarative pipeline…