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