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

Support run containers as components

XMLWordPrintable

      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)

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

              Created:
              Updated:
              Resolved: