Search code examples
jenkinsdocker-registryjenkins-job-dsl

how do i add authentication credentials to docker pipeline in jenkins?


I am familair with how to use the docker pipeline to run jobs in docker containers on jenkins but so far it works great for public docker images that do not require any authentication credentials to pull images from a registry for example

https://jenkins.io/doc/book/pipeline/docker/

so lets say i have this for example

pipeline {
    agent {
        docker { 
                 image 'private.docker.local/node:7-alpine' 
                 args '--net=host  -u root'
               }
    }
    stages {
        stage('Test') {
            steps {
                sh 'node --version'
            }
        }
    }
}

when i tried that i get error

+ docker pull private.docker.local/node:7-alpine

Error response from daemon: Get https://private.docker.local/v2/node:7-alpine/manifests/7-alpine: denied: access forbidden

anyone aware of how to add auth so that the docker pipeline can login to docker registry to pull images?


Solution

  • Actually finally found answer here https://jenkins.io/doc/book/pipeline/syntax/#agent

    pipeline {
        agent {
            docker { 
                     alwaysPull true
                     image 'private.docker.local/node:7-alpine' 
                     args '--net=host  -u root'
                     registryUrl 'https://private.docker.local/'
                     registryCredentialsId 'registry-credential'
                   }
        }
        stages {
            stage('Test') {
                steps {
                    sh 'node --version'
                }
            }
        }
    }
    

    enjoy