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

Run stages in separate Docker containers when top level agent declared

XMLWordPrintable

      When specifying either docker or dockerfile as the top level agent this will run the entire pipeline and all of it's stages in the same container instance created at the beginning.

      pipeline {
          agent {
              docker {
                  image 'python3.6'
              }
          }
          /* These stages all run in the same python3.6 container
           * created at the beginning of the pipeline.
           */
          stages {
              stage('Stage 1') {
                  steps {
                      sh 'echo foo'
                  }
              }
              stage('Stage 2') {
                  steps {
                      sh 'echo bar'
                  }
              }
          }
      }
      

       
      It would be nice if there were some setting to run each stage in it's own container instance.

      pipeline {
          agent {
              docker {
                  image 'python3.6'
                  runPerStage true
              }
          }
          /* These stages all create their own python3.6 container
           * created at the beginning of each stage.
           */
          stages {
              stage('Stage 1') {
                  steps {
                      sh 'echo foo'
                  }
              }
              stage('Stage 2') {
                  steps {
                      sh 'echo bar'
                  }
              }
          }
      }
      

      Additionally, it would be nice to declare on a per stage basis if a new container instance is needed while other stages can use the instance created at the top level.

      pipeline {
          agent {
              docker {
                  image 'python3.6'
              }
          }
          stages {
              /* This stage creates it's own python3.6 container instance. */
              stage('Stage 1') {
                  agent {
                      docker {
                          createNew true
                      }
                  }
                  steps {
                      sh 'echo foo'
                  }
              }
              /* This stage uses the python3.6 container instance created
               * at the top level.
               */
              stage('Stage 2') {
                  steps {
                      sh 'echo bar'
                  }
              }
          }
      }
      

      Finally, when declaring a top level docker or dockerfile agent and later specifying a stage level docker or dockerfile agent Jenkins will try and do a docker-in-docker type thing breaking the pipeline and doesn't seem to handle this at all.

      It would be nice to have the top level agent as the default container used for stages but if a stage declares it's own agent than that container is used instead. This seem to match more closely to how agent works when not using docker or dockerfile.

      pipeline {
          agent {
              docker {
                  image 'python3.6'
              }
          }
          stages {
              /* This stage creates it's own python2.7 container instance. */
              stage('Stage 1') {
                  agent {
                      docker {
                          image 'python2.7'
                      }
                  }
                  steps {
                      sh 'echo foo'
                  }
              }
              /* This stage uses the python3.6 container instance created
               * at the top level.
               */
              stage('Stage 2') {
                  steps {
                      sh 'echo bar'
                  }
              }
          }
      }
      

            abayer Andrew Bayer
            awiddersheim Andrew Widdersheim
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: