1

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"
    }
}
}
4
  • Where would you like to run docker-compose, e.g. on jenkins or on a remote system?
    – 030
    Mar 26 '18 at 18:34
  • 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).
    – Alex Hampu
    Mar 26 '18 at 18:35
  • 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?
    – 030
    Mar 26 '18 at 18:41
  • 1
    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.
    – Alex Hampu
    Mar 26 '18 at 18:46
0

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.

1
  • It works, but is this the only possible option?
    – Alex Hampu
    Mar 27 '18 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.