Tim
Tim

Reputation: 643

Push Docker Repository to Dockerhub with Jenkins

we want to push an docker repository to DockerHub - from the shell is works. But in Jenkins we get the error message "errorDetail":{"message":"unauthorized: access to the requested resource is not authorized"

I think the problem is that in shell (docker login) i have to insert the email adress, login and password. In Jenkins i can only set login and password NO email. The version of credential plugin is 1.24 and we use docker-build-step for the docker steps.

thx

Upvotes: 4

Views: 10283

Answers (2)

gjseminario
gjseminario

Reputation: 71

Maybe you can use Docker pipeline plugin (it comes in the recommended plugins).

Jenkinsfile example:

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

Upvotes: 2

Bruno Lavit
Bruno Lavit

Reputation: 10382

Can you have a try with the CloudBees Docker Build and Publish plugin?

This plugin allows to create a build step to build a Dockerfile and publish the image into a registry (DockerHub or a private registry):

enter image description here

Another solution is to open a session on your Jenkins machine with the jenkins user + login to DockerHub with the relevant credentials?

With this solution, the DockerHub credentials will be cached and Jenkins should be able to push your images to the DockerHub registry.

Upvotes: 2

Related Questions