title: Using the Jenkins pipeline docker plugin, how to pull an image and push a new tag to it?
body: <p>I have an image already in my private repo. I need to pull that image, create a tag and push it to the registry. </p>
  
  <p><strong>What's the bes way to do this using the Jenkins WithRegistry ?</strong></p>
  
  <p>Here's my actual code:</p>
  
  <pre><code>        stage("Applying to docker repo") {
          steps {
              script {
                def imageNameLookup = configs.dockerRegistry.repo + "/"+repo.toLowerCase()+":"+params.versionToTag
                echo 'looking up '+ imageNameLookup
                docker.withRegistry('https://' + configs.dockerRegistry.url, configs.dockerRegistry.credentialsId) {
  
                  try {
                    image = docker.image(repo.toLowerCase()+":"+params.versionToTag).pull()
                    image.tag("${deliveryTag}")
                    image.push()
                  } catch (Exception e) {
                    echo ' catch 2 '+ e.getMessage()
                  }
                 }
              }
          }
      }
  </code></pre>
  
  <p>When running the image.tag(), i get the following error :</p>
  
  <blockquote>
    <p>bfc9288fe86d: Pull complete
    Digest: sha256:ee9b01eb62f2f21dcb3bf4af285702c8991d1789e659515fdfa2da2619f1d8b9
    Status: Downloaded newer image for repodocker-xxx.xxx.xx/my-api:1.19.0
    [Pipeline] echo
     catch 2 Cannot invoke method tag() on null object</p>
  </blockquote>
  
  <p><strong>EDIT:</strong>
  I was able to pull the image but when I try to create the tag, I get a new error : <strong>No such image :latest</strong></p>
  
  <p>I don't need to set the tag latest because I'm tagging another version.</p>
  
  <blockquote>
  <pre><code>              docker.withRegistry('https://' + configs.dockerRegistry.url, configs.dockerRegistry.credentialsId) {
  </code></pre>
  </blockquote>
  
  <pre><code>                try {
                    docker.image(repo.toLowerCase()+":"+params.versionToTag).pull()
                    sh "docker tag ${repo.toLowerCase()} ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                    sh ""
                   )
                  } catch (Exception e) {
                    echo ' catch 2 '+ e.getMessage()
                  }
  </code></pre>
  
  <p>and my new log :</p>
  
  <pre><code>[Pipeline] sh
  + docker pull repodocker-xxxx.xxx.xx/myapi-api:1.19.0
  1.19.0: Pulling from myapi-api
  Digest: sha256:ee9b01eb62f2f21dcb3bf4af285702c8991d1789e659515fdfa2da2619f1d8b9
  Status: Image is up to date for repodocker-xxxx.xxx.xx/myapi-api:1.19.0
  [Pipeline] sh
  + docker tag myapi-api grdocker-xxxx.xx.xx:443/xx.xxx.xxx/myapi-api:testTag20
  Error response from daemon: No such image: myapi-api:latest
  [Pipeline] echo
   catch 2 script returned exit code 1
  </code></pre>
  
  <p><strong>EDIT2</strong>
  was able to do it by doing it this way : </p>
  
  <pre><code>        stage("Applying to docker repo") {
          steps {
              script {
                  docker.withRegistry('https://' + configs.dockerRegistry.url, configs.dockerRegistry.credentialsId) {
                    docker.image(repo.toLowerCase()+":"+params.versionToTag).pull()
                    sh "docker tag ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${params.versionToTag} ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                    sh "docker push ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                  }
              }
          }
      }
  </code></pre>
  
  <p><strong>FINAL EDIT</strong>
  Here's the final solution in Jenkins and the docker plugin which was not able to do everything. </p>
  
  <pre><code>        stage("Applying to docker repo") {
          steps {
              script {
                    docker.withRegistry('https://' + configs.dockerRegistry.url, configs.dockerRegistry.credentialsId) {
                    docker.image(repo.toLowerCase()+":"+params.versionToTag).pull()
                    sh "docker tag ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${params.versionToTag} ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                    sh "docker push ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                    docker.image(repo.toLowerCase()+":${deliveryTag}").pull()
                  }
              }
          }
      }
  </code></pre>