Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a Jenkins pipeline job to pull a new private image from Dockerhub

I want to pull a new version of a specific private image with a specific tag on Docker Hub using the Jenkins Docker pipeline plugin. The Docker shell commands would look like:

docker login -u user -p password
docker pull user/foo:bar

Something like this seems like it ought to work:

node () {
    image = docker.image('user/foo:bar')
    image.pull()
    image.inside {
        // Commands to run in the container

But there's no way to execute the login step, so I always get the error:

Error response from daemon: pull access denied for user/foo, repository does not exist or may require 'docker login'.

I've gone through the documentation and the code, but the documentation doesn't even mention pulling and the examples in the code don't show how to log in to pull a private image. I can manually script it but the whole point of using the Docker pipeline plugin is to avoid directly scripting Docker commands.

like image 307
ceridwen Avatar asked Jan 04 '18 23:01

ceridwen


People also ask

How do I pull an image from a private repository?

In order to pull images from your private repository, you'll need to login to Docker. If no registry URI is specified, Docker will assume you intend to use or log out from Docker Hub. Triton comes with several images built-in. You can view the available list with triton images .

Do I need to login to Docker Hub to pull the images?

Run docker run <your_username>/my-private-repo to test your Docker image locally. You must be signed in to Docker Hub through Docker Desktop or the command line, and you must also name your images correctly, as per the above steps.

How does Jenkins integrate with Docker Hub?

In Jenkins you have to add a new credential with your Docker Hub account details. Go to Credentials → Global → Add credentials and fill out the form with your username and password. Create your Jenkins pipeline.

How do I set Docker Hub credentials in Jenkins?

First, we need to add Docker hub username and token to the security credentials of the Jenkins server. So go to Manage Jenkins -> Manage Credentials -> Domains(global) -> Add Credentials. Alternatively, you can use the below URL to add credentials. And change the IP address.


1 Answers

I believe what you will need here is the withRegistry function. It can be used as follows

docker.withRegistry('<registry-url>', '<credential-id>') {
    image = docker.image('user/foo:bar')
    image.pull()
}

Everything within the withRegistry block will use that registry and authentication for pulling the image.

Where the <registry-url> in this case is the URL for the Dockerhub registry. I believe the Dockerhub registry URL is https://registry.hub.docker.com

The <credential-id> is the ID of the Dockerhub credentials stored in Jenkins.

To add these credentials navigate from the Jenkins index page to Credentials -> System -> Global credentials -> Add Credentials.

Once on this page you will need to select Kind as Username with password. The scope should be Global. The username and password fields are your username and password for Dockerhub.

The ID field is whatever text you want to be the <credential-id>. For example if you make the ID field docker-hub-credentials that will need to be the second argument to withRegistry.

Here is an example of what the page to add credentials could look like. enter image description here

like image 141
Matt McCoy Avatar answered Sep 20 '22 17:09

Matt McCoy