Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple docker repositories in a Jenkins pipeline

I have a Jenkins pipeline in which I need to log into two different docker repositories. I know how to authenticate to one repo using the following command

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

but don't know how to do it for more than 1 repo?

like image 336
HHH Avatar asked Jul 23 '19 15:07

HHH


People also ask

How many private repositories are allowed for a individual on Docker Hub?

How many private repositories are allowed for an individual on Docker hub? Answer : 1.

How do I make Docker images automatically with Jenkins pipeline?

Manage Jenkins → Manage Plugins. Search Docker Pipelines, click on Install without restart and wait until is done. Upload your Dockerfile definition to your Github repository. Click on the green button Clone or Download and copy the URL as you will need it later.

How do I run multiple pipelines in Jenkins?

To create a Multi-branch Pipeline: Click New Item on your Jenkins home page, enter a name for your job, select Multibranch Pipeline, and click OK.


2 Answers

Nesting docker.withRegistry calls actually works as expected. Each call adds an entry to /home/jenkins/.dockercfg with provided credentials.

// Empty registry ('') means default Docker Hub `https://index.docker.io/v1/`
docker.withRegistry('', 'dockerhub-credentials-id') {
  docker.withRegistry('https://private-registry.example.com', 'private-credentials-id') {
    // your build steps ...
  }
}

This allows you to pull base images from Docker Hub using provided credentials to avoid recently introduced pull limits, and push results into another docker registry.

like image 177
Alexey Rogulin Avatar answered Sep 22 '22 13:09

Alexey Rogulin


This is a partial answer only applicable when you are using two registries but only need credentials on one. You can nest the calls since they mostly just do a docker login that stays active for the scope of the closure and will add the registry domain name into docker pushes and such.

Use this in a scripted Jenkins pipeline or in a script { } section of a declarative Jenkins pipeline:

docker.withRegistry('https://registry.example1.com') { // no credentials here
    docker.withRegistry('https://registry.example2.com', 'credentials-id') { // credentials needed
        def container = docker.build('myImage')
        container.inside() {
            sh "ls -al" // example to run on the newly built 
        }
    }
}

Sometimes you can use two, non-nested calls to docker.withRegistry() one after the other but building is an example of when you can't if, for example, the base image for the first FROM in the Dockerfile needs one registry and the base image for a second FROM is in another registry.

like image 35
Lee Meador Avatar answered Sep 18 '22 13:09

Lee Meador