Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-46277

Latest docker image is not pulled when used within stage

    Details

    • Similar Issues:

      Description

      When a docker image is used as an agent at the pipeline level, `image.pull()` is called automatically to check for a newer version. E.g.:

      pipeline {
        agent { docker 'microsoft/dotnet' }
        stages {
          stage('x') {
            steps {
              sh '...'
            }
          }
        }
      }
      

      However, when a docker image is used as an agent at the stage level, `image.pull()` is never called, so potentially old images are used instead.E.g.:

      pipeline {
        agent none
        stages {
          stage('x') {
            agent { docker 'microsoft/dotnet' }
            steps {
              sh '...'
            }
          }
        }
      }
      

      Is it possible to automatically call `image.pull()` as well, or at least provide an option flag to state you want this behaviour?

      I believe the code that affects this is: https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/824b3b7a96cc9d4fc6fc636caccaed3d39343cfd/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineScript.groovy#L42

        Attachments

          Issue Links

            Activity

            chris5287 Chris Stylianou created issue -
            abayer Andrew Bayer made changes -
            Field Original Value New Value
            Status Open [ 1 ] In Progress [ 3 ]
            abayer Andrew Bayer made changes -
            Status In Progress [ 3 ] In Review [ 10005 ]
            Hide
            abayer Andrew Bayer added a comment -

            Well, that was dumb. Sorry about that - PR up at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/182, this'll go into Declarative 1.2.

            Show
            abayer Andrew Bayer added a comment - Well, that was dumb. Sorry about that - PR up at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/182 , this'll go into Declarative 1.2.
            abayer Andrew Bayer made changes -
            Remote Link This issue links to "PR #182 (Web Link)" [ 17476 ]
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Andrew Bayer
            Path:
            pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineScript.groovy
            http://jenkins-ci.org/commit/pipeline-model-definition-plugin/4a1df3d70a89ab394bc3ecf78b0468a2dccc71c0
            Log:
            [FIXED JENKINS-46277] Do a pull for stage-level docker agent too

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineScript.groovy http://jenkins-ci.org/commit/pipeline-model-definition-plugin/4a1df3d70a89ab394bc3ecf78b0468a2dccc71c0 Log: [FIXED JENKINS-46277] Do a pull for stage-level docker agent too
            Hide
            abayer Andrew Bayer added a comment -

            Fixed in 1.2.

            Show
            abayer Andrew Bayer added a comment - Fixed in 1.2.
            abayer Andrew Bayer made changes -
            Status In Review [ 10005 ] Resolved [ 5 ]
            Resolution Fixed [ 1 ]
            Hide
            scottles Scott Hilleard added a comment -

            Our declarative Pipelines rely on using locally built images from earlier in the pipeline and now all our pipelines are broken after updating to 1.2. Please revert this change or make it optional.

             

            Show
            scottles Scott Hilleard added a comment - Our declarative Pipelines rely on using locally built images from earlier in the pipeline and now all our pipelines are broken after updating to 1.2. Please revert this change or make it optional.  
            Hide
            scottles Scott Hilleard added a comment -

            This has caused a regression for us. Our pipelines no longer work with 1.2, as we build local images during the pipeline run and now it will not use our local images because it tries to pull them from docker.io. Pulling the latest image should be an option, not mandatory.

            Show
            scottles Scott Hilleard added a comment - This has caused a regression for us. Our pipelines no longer work with 1.2, as we build local images during the pipeline run and now it will not use our local images because it tries to pull them from docker.io. Pulling the latest image should be an option, not mandatory.
            scottles Scott Hilleard made changes -
            Resolution Fixed [ 1 ]
            Status Resolved [ 5 ] Reopened [ 4 ]
            Hide
            chris5287 Chris Stylianou added a comment -

            Scott Hilleard do you have an example pipeline where this breaks? If you are using docker images with labels that could only exist locally, I wouldn't have thought this change would break your builds, as there is nothing to pull from docker.io?

            Show
            chris5287 Chris Stylianou added a comment - Scott Hilleard do you have an example pipeline where this breaks? If you are using docker images with labels that could only exist locally, I wouldn't have thought this change would break your builds, as there is nothing to pull from docker.io?
            Hide
            scottles Scott Hilleard added a comment - - edited

            Hi Chris,

            Here is a sample from our pipeline. We build a temporary version of our container for various building of assets in the workspace and running tests. Later these are then copied and the final container is built and pushed. The example below now causes the "Composer install for unit tests" to fail as it will try and pull the "web_base" container from docker.io as of the 1.2 update (it previously used to use the local version).

             

            pipeline {
              stages {
                stage('Build temporary web_base for composer') {
                   agent any
                   steps {
                     sh 'docker build -t web_base:$BUILD_ID docker/web_base'
                     sh 'cp /usr/local/bin/composer $WORKSPACE'
                   }
                 }
                 stage('Composer install for unit tests') {
                   agent {
                     docker {
                       image 'web_base:$BUILD_ID'
                       args '-v $WORKSPACE:/var/www'
                     }
                   }
                   steps {
                     echo 'Installing composer'
                     sh 'cd /var/www && ./composer install'
                   }
                 }
              }
            }
            

             

            Show
            scottles Scott Hilleard added a comment - - edited Hi Chris, Here is a sample from our pipeline. We build a temporary version of our container for various building of assets in the workspace and running tests. Later these are then copied and the final container is built and pushed. The example below now causes the "Composer install for unit tests" to fail as it will try and pull the "web_base" container from docker.io as of the 1.2 update (it previously used to use the local version).   pipeline {   stages {     stage( 'Build temporary web_base for composer' ) {       agent any       steps {      sh 'docker build -t web_base:$BUILD_ID docker/web_base'      sh 'cp /usr/local/bin/composer $WORKSPACE'       }     }     stage( 'Composer install for unit tests' ) {       agent {      docker {       image 'web_base:$BUILD_ID'       args '-v $WORKSPACE:/ var /www'     }       }       steps {     echo 'Installing composer'     sh 'cd / var /www && ./composer install'       }     }   } }  
            Hide
            scottles Scott Hilleard added a comment - - edited

            Hopefully the sample will give you an idea. EDIT - Fixed above comment

            Show
            scottles Scott Hilleard added a comment - - edited Hopefully the sample will give you an idea. EDIT - Fixed above comment
            Hide
            chris5287 Chris Stylianou added a comment -

            Hmm... why not instead use a Dockerfile agent directly instead?

            agent { dockerfile { dir 'docker/web_base' } }
            

            See: https://jenkins.io/doc/book/pipeline/syntax/#agent

            Show
            chris5287 Chris Stylianou added a comment - Hmm... why not instead use a Dockerfile agent directly instead? agent { dockerfile { dir 'docker/web_base' } } See: https://jenkins.io/doc/book/pipeline/syntax/#agent
            Hide
            scottles Scott Hilleard added a comment -

            Because we need to mount the workspace inside the container and you can not mount a volume with the dockerfile agent.

             

            Show
            scottles Scott Hilleard added a comment - Because we need to mount the workspace inside the container and you can not mount a volume with the dockerfile agent.  
            Hide
            chris5287 Chris Stylianou added a comment -

            Sorry missed your args but the following should work:

            agent {
              dockerfile {
                dir 'docker/web_base'
                args '-v $WORKSPACE:/var/www'
              }
            }
            

            See: https://stackoverflow.com/questions/44805076/setting-build-args-for-dockerfile-agent-using-a-jenkins-declarative-pipeline#answer-46359650

            Show
            chris5287 Chris Stylianou added a comment - Sorry missed your args but the following should work: agent { dockerfile { dir 'docker/web_base' args '-v $WORKSPACE:/ var /www' } } See: https://stackoverflow.com/questions/44805076/setting-build-args-for-dockerfile-agent-using-a-jenkins-declarative-pipeline#answer-46359650
            Hide
            scottles Scott Hilleard added a comment -

            If that works, it is not presently documented as a feature and probably should be! I will give that a try (though I still think the option to pull an image should be an option of the agent, even if it is the default). Thanks!

             

            Show
            scottles Scott Hilleard added a comment - If that works, it is not presently documented as a feature and probably should be! I will give that a try (though I still think the option to pull an image should be an option of the agent, even if it is the default). Thanks!  
            Hide
            scottles Scott Hilleard added a comment -

            Chris, just to confirm yes the "args" parameter does indeed work with the Dockerfile agent and we can map in volumes. That really should be in the dockerfile agent documentation as it is a very useful feature! Thank you for your help!

             

            Show
            scottles Scott Hilleard added a comment - Chris, just to confirm yes the "args" parameter does indeed work with the Dockerfile agent and we can map in volumes. That really should be in the dockerfile agent documentation as it is a very useful feature! Thank you for your help!  
            chris5287 Chris Stylianou made changes -
            Status Reopened [ 4 ] Open [ 1 ]
            chris5287 Chris Stylianou made changes -
            Status Open [ 1 ] Resolved [ 5 ]
            Resolution Fixed [ 1 ]
            Hide
            chris5287 Chris Stylianou added a comment -

            Scott Hilleard glad I was able to help!

            I suggest you raise new tickets to individually track adding an optional auto-pull for agents/updating the docker documentation

            Show
            chris5287 Chris Stylianou added a comment - Scott Hilleard  glad I was able to help! I suggest you raise new tickets to individually track adding an optional auto-pull for agents/updating the docker documentation
            Hide
            bitwiseman Liam Newman added a comment -

            Bulk closing resolved issues.

            Show
            bitwiseman Liam Newman added a comment - Bulk closing resolved issues.
            bitwiseman Liam Newman made changes -
            Status Resolved [ 5 ] Closed [ 6 ]

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                chris5287 Chris Stylianou
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: