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

Support run containers as components

    Details

    • Similar Issues:

      Description

      def rootNode = "investigate"
      def containerRunOptions = [
          "-v /var/run/docker.sock:/var/run/docker.sock",
         // well by default plugin runs with -u 1000:1000 flag which could break using docker inside container as given user won't have proper group access
          "-u 1000:docker"
      ].join(" ")
      
      def prepareDockerEnvironment(){
          try {
              // wipe out old unused docker networks
              sh("docker network ls --filter name=jenkins -q | xargs docker network rm")
          }
          catch (error){
              echo "no previous jenkins networks found."
          }
          finally {
              // create isolated docker network for current run
              sh("docker network create --driver bridge jenkins-${env.BUILD_TAG}")
          }
      }
      
      // extends options with strict network binding, to get an access for services via standard
      // names via name resolving
      def opts(String options){
          return [options, "--net jenkins-${env.BUILD_TAG}"].join(" ")
      }
      
      node(rootNode){
          def pgImage = "postgres:9.4"
          def dockerEnv = docker.image("ci-image")  // replaced with fake image
          def postgresEnv = docker.image("postgres:9.4")
      
          stage("prepare environment"){
              sh 'ls -la'
              prepareDockerEnvironment()
          }
      
          dockerEnv.inside(opts(containerRunOptions)){
              stage("start services"){
                  docker.image(pgImage).withRun(opts("--name database")){ pgContainer ->
                      echo "${pgContainer.id}"
                      sh 'ls -la '
                      sh "nc -z database 5432"
                  }
              }
          }
      }
      

      The main idea of given ticket is to support running some tasks inside docker container with some network or may be even volume resources which should be run outside testing container.

      Snippet works fine to me despite it looks kind a verbosy and has `bad` knowledge about some docker infrastructure such as networking setup.
      However if I need for example memcache and / or redis I should append one more closure to run containers and that will look pretty badly for anyone. I'll skip an example for everyone's sake.

      Simple idea is to make possibility to run such containers with one closure scope for example.

      Real helpful thing would be similar to gitlab-ci services functionality (https://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-a-service)

        Attachments

          Activity

          Hide
          jglick Jesse Glick added a comment -

          Do not really follow, but Jenkins is unlikely to implement anything like this. You may be able to create libraries or add-on plugins for it.

          Show
          jglick Jesse Glick added a comment - Do not really follow, but Jenkins is unlikely to implement anything like this. You may be able to create libraries or add-on plugins for it.
          Hide
          nickolasfox Nickolas Fox added a comment -

          Jesse Glick isn't it a docker-workflow-plugin related issue? The topic is not about jenkins itself, it's about existent plugin that works they way described above, but in general it looks pretty sophisticated and hard to maintain.

          Show
          nickolasfox Nickolas Fox added a comment - Jesse Glick isn't it a docker-workflow-plugin related issue? The topic is not about jenkins itself, it's about existent plugin that works they way described above, but in general it looks pretty sophisticated and hard to maintain.
          Hide
          jglick Jesse Glick added a comment -

          I actually have no idea what you are talking about here or what you are asking for.

          Show
          jglick Jesse Glick added a comment - I actually have no idea what you are talking about here or what you are asking for.
          Hide
          nickolasfox Nickolas Fox added a comment - - edited

          Ok, I'll start right from the beggining

          For example I'd like run a tests with following services enabled:

          • postgresql
          • mongo
          • redis
          • memcache

          Here a snippet:

          // ... node setup, scm, bla-bla-bla
          
          
          dockerEnv = docker.image("ci-image")
          dockerEnv.inside(){
              stage("start services"){
                  // start postgresql
                  docker.image("postgresql:9.4").withRun("--name database"){ pgContainer ->
                      echo "${pgContainer.id}"
                      // run redis
                      docker.image("redis:latest").withRun("--name redis") {
                          // run mongo
                          docker.image("mongo:latest").withRun("--name mongo"){
                              // etc ...
                              docker.image("memcache:latest").withRun("--name memcache"){
                                  sh "nc -z database 5432"
                                  sh "nc -z mongo 21017"
                                  sh "nc -z redis 6379"
                              }
                          }
                      }
                  }
              }
          }
          

          Does it make sense to use some flat configuration for such containers run?

          Show
          nickolasfox Nickolas Fox added a comment - - edited Ok, I'll start right from the beggining For example I'd like run a tests with following services enabled: postgresql mongo redis memcache Here a snippet: // ... node setup, scm, bla-bla-bla dockerEnv = docker.image( "ci-image" ) dockerEnv.inside(){ stage( "start services" ){ // start postgresql docker.image( "postgresql:9.4" ).withRun( "--name database" ){ pgContainer -> echo "${pgContainer.id}" // run redis docker.image( "redis:latest" ).withRun( "--name redis" ) { // run mongo docker.image( "mongo:latest" ).withRun( "--name mongo" ){ // etc ... docker.image( "memcache:latest" ).withRun( "--name memcache" ){ sh "nc -z database 5432" sh "nc -z mongo 21017" sh "nc -z redis 6379" } } } } } } Does it make sense to use some flat configuration for such containers run?
          Hide
          jglick Jesse Glick added a comment -

          Oh. I think what you are looking for is Docker Compose. There is no special support for it in this plugin, beyond withRegistry/withServer/withTool.

          Show
          jglick Jesse Glick added a comment - Oh. I think what you are looking for is Docker Compose. There is no special support for it in this plugin, beyond withRegistry / withServer / withTool .
          Hide
          nickolasfox Nickolas Fox added a comment -

          It's not quite that simple to use docker compose with pipeline plugin. Ok I'll just switch to gitlab.

          Thanks anyway.

          Show
          nickolasfox Nickolas Fox added a comment - It's not quite that simple to use docker compose with pipeline plugin. Ok I'll just switch to gitlab. Thanks anyway.
          Hide
          jglick Jesse Glick added a comment -

          Wrap your calls to docker-compose up, down, and whatever else into a Python script or whatever else you like (which you can trivially test offline) and call it from sh.

          Show
          jglick Jesse Glick added a comment - Wrap your calls to docker-compose up , down , and whatever else into a Python script or whatever else you like (which you can trivially test offline) and call it from sh .
          Hide
          nickolasfox Nickolas Fox added a comment -

          Thank you, I know how to use docker-compose. It's kind a bypass the limitations of the plugin. I do not want to bring a bypasses or hacks into working pipeline. Anyway let's forget about it.

          The first snippet does that docker-compose configuration without docker-compose, it just looks ugly but it's working. To get the full idea I was talking about you might want to see how gitlab-ci over docker environment works (:

          Anyway the ticket is closed, following conversation is pointless.

          Show
          nickolasfox Nickolas Fox added a comment - Thank you, I know how to use docker-compose. It's kind a bypass the limitations of the plugin. I do not want to bring a bypasses or hacks into working pipeline. Anyway let's forget about it. The first snippet does that docker-compose configuration without docker-compose, it just looks ugly but it's working. To get the full idea I was talking about you might want to see how gitlab-ci over docker environment works (: Anyway the ticket is closed, following conversation is pointless.

            People

            • Assignee:
              Unassigned
              Reporter:
              nickolasfox Nickolas Fox
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: