Home > other >  Pulling docker image in jenkins with using declarative Jenkinsfile
Pulling docker image in jenkins with using declarative Jenkinsfile

Time:01-13

I've created jenkins job with Jenkinsfile configuration which should be executed in docker container. There is a problem with pulling docker image from docker hub: node:16-stretch I've received error like: Error response from daemon: Get enter image description here

Below I show part of my Jenkinsfile responsible for pulling docker image in declarative approach:

stages {
        stage('Docker container initialize') {
             agent {
                docker {
                   image 'node:16-stretch'
                   reuseNode true
                }
             }
             steps {
                   sh 'node --version'
                   sh 'yarn --version'
             }
         }

I have requirement to run job with node version >=14.18.0 and yarn >=1.22.0. Image node:16-stretch meet this requirements. How should I configure correctly my jenkinsfile in order to pull this above image? Should I add some credentials in job configuration or maybe in Jenkinsfile script? I would be grateful for help :)

CodePudding user response:

You need to add the registry credentials in the credentials section of the Jenkins server. You also should ensure this pipeline has permissions to access those credentials. After completing that task, you can access the credentials in the agent directive like:

agent {
  docker {
    image                 'node:16-stretch'
    reuseNode             true
    registryUrl           'https://registry-1.docker.io'
    registryCredentialsId 'myRegistryCredentials'
  }
}

where myRegistryCredentials is the name of the credentials you added for the registry.

  •  Tags:  
  • Related