Answers: <p>One way to solve this it to use multiple pipelines. The first pipeline runs the test in docker. When that has been completed, one could call another job</p>
  
  <p><a href="https://jenkins.io/doc/pipeline/steps/pipeline-build-step/" rel="nofollow noreferrer">https://jenkins.io/doc/pipeline/steps/pipeline-build-step/</a></p>
  
  <p><a href="https://support.cloudbees.com/hc/en-us/articles/221400287-How-to-pass-parameter-to-downstream-job-in-Pipeline-job-" rel="nofollow noreferrer">https://support.cloudbees.com/hc/en-us/articles/221400287-How-to-pass-parameter-to-downstream-job-in-Pipeline-job-</a></p>
  
  <blockquote>
  <pre><code>node {
      paramAValue = "paramAValue"
      paramBValue = "paramBValue"
      build job: 'downstream-freestyle', parameters: [[$class:  'StringParameterValue', name: 'ParamA', value: paramAValue], [$class:
         'StringParameterValue', name: 'ParamB', value: paramBValue]]
  }
  </code></pre>
  </blockquote>
  
  <p>that runs docker-compose on Jenkins itself.</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-03-27T06:21:08.317Z
Answers: It works, but is this the only possible option?
Answers: Alex Hampu
Answers: CC BY-SA 3.0
Answers: 2018-03-26T18:57:40.033Z
Answers: 2018-03-26T18:57:40.033Z
Answers: 030
Body: <p>I have a CI server which has Jenkins installed in the host it self, and I want to run builds and tests inside Docker containers. Until here it works like it should, but I also want to deploy containers using docker-compose and this should be done in the host, but when I run commands inside the Jenkinsfile, they are ran inside the Docker container used for builds and tests.</p>
  
  <pre><code>pipeline {
  agent {
      dockerfile {
          filename "Dockerfile"
      }
  }
  
  stages {
      stage("Run unit tests") {
          steps {
              # This is ran inside the Docker container, as expected
              sh "cp environments/.env.build .env"
              sh "vendor/bin/phpunit"
          }
      }
  
      stage('Deploy to test') {
          when {
              branch "develop"
          }
  
          steps {
              # This is still ran inside the Docker container, not the host
              sh 'docker-compose up -d'
          }
      }
  
      stage("Archive") {
          steps {
              # This is ran inside the Docker container, as expected
              sh "zip -r dist.zip ."
          }
      }
  }
  
  post {
      always {
          archiveArtifacts artifacts: "dist.zip"
          junit "reports/phpunit.report.xml"
      }
  }
  }
  </code></pre>
  
Comments: CC BY-SA 3.0
Comments: 2018-03-26T18:34:07.120Z
Comments: Where would you like to run docker-compose, e.g. on jenkins or on a remote system?
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2018-03-26T18:35:42.620Z
Comments: `docker-compose` should be ran by Jenkins on the host system (so local, not remote) and not inside the Docker container (Jenkins doesn't run in a container, it's installed on the host).
Comments: Alex Hampu
Comments: CC BY-SA 3.0
Comments: 2018-03-26T18:41:35.607Z
Comments: It is not clear to me why you would like to run docker-compose inside a docker container. Could you explain why you do not run the image directly on the Jenkins host?
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2018-03-26T18:46:02.873Z
Comments: I do not want to run `docker-compose` inside a Docker container, this is what happens at the moment, which is not the desired result.
  
  The process is the following
  - Build starts
  - Inside the build a Docker container starts, and inside it tests are executed
  - At this point the tests ar finished, from here on, we should be out of the Docker container. Next step is to deploy a container using docker-compose, on the host machine (the machine where Jenkins is installed).
  
  This is where I'm having issues, as the `docker-compose` command is ran inside the Docker container.
Comments: Alex Hampu
Tags: <docker><jenkins>
Title: Jenkins pipeline + Docker integration
UserDisplayName: Alex Hampu
ThreadText: # Jenkins pipeline + Docker integration
  
  (Asked by: username_0 on 2018-03-26)
  
  I have a CI server which has Jenkins installed in the host it self, and I want to run builds and tests inside Docker containers. Until here it works like it should, but I also want to deploy containers using docker-compose and this should be done in the host, but when I run commands inside the Jenkinsfile, they are ran inside the Docker container used for builds and tests.
  
  ```
  pipeline {
  agent {
      dockerfile {
          filename "Dockerfile"
      }
  }
  
  stages {
      stage("Run unit tests") {
          steps {
              # This is ran inside the Docker container, as expected
              sh "cp environments/.env.build .env"
              sh "vendor/bin/phpunit"
          }
      }
  
      stage('Deploy to test') {
          when {
              branch "develop"
          }
  
          steps {
              # This is still ran inside the Docker container, not the host
              sh 'docker-compose up -d'
          }
      }
  
      stage("Archive") {
          steps {
              # This is ran inside the Docker container, as expected
              sh "zip -r dist.zip ."
          }
      }
  }
  
  post {
      always {
          archiveArtifacts artifacts: "dist.zip"
          junit "reports/phpunit.report.xml"
      }
  }
  }
  
  ```
  
  **Tags:** docker, jenkins
  
  > **Comments:**
  > **username_1**: Where would you like to run docker-compose, e.g. on jenkins or on a remote system?  
  > **username_0**: `docker-compose` should be ran by Jenkins on the host system (so local, not remote) and not inside the Docker container (Jenkins doesn't run in a container, it's installed on the host).  
  > **username_1**: It is not clear to me why you would like to run docker-compose inside a docker container. Could you explain why you do not run the image directly on the Jenkins host?  
  > **username_0**: I do not want to run `docker-compose` inside a Docker container, this is what happens at the moment, which is not the desired result.  The process is the following - Build starts - Inside the build a Docker container starts, and inside it tests are executed - At this point the tests ar finished, from here on, we should be out of the Docker container. Next step is to deploy a container using docker-compose, on the host machine (the machine where Jenkins is installed).  This is where I'm having issues, as the `docker-compose` command is ran inside the Docker container.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-26. Score: 0 | Accepted answer
  
  One way to solve this it to use multiple pipelines. The first pipeline runs the test in docker. When that has been completed, one could call another job
  
  <https://jenkins.io/doc/pipeline/steps/pipeline-build-step/>
  
  <https://support.cloudbees.com/hc/en-us/articles/221400287-How-to-pass-parameter-to-downstream-job-in-Pipeline-job->
  
  > ```
  > node {
  >     paramAValue = "paramAValue"
  >     paramBValue = "paramBValue"
  >     build job: 'downstream-freestyle', parameters: [[$class:  'StringParameterValue', name: 'ParamA', value: paramAValue], [$class:
  >        'StringParameterValue', name: 'ParamB', value: paramBValue]]
  > }
  >
  > ```
  
  that runs docker-compose on Jenkins itself.
  
  > **Comments:**
  > **username_0**: It works, but is this the only possible option?  
  
  ---
  
  