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.
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 .
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With