Not getting Docker to run from within Jenkins

2019-09-16 12:28发布

问题:

I have a Jenkins-Pipeline that successfully builds a docker-image. But when it comes to running the docker-image it fails. Inside the docker image I have some API-tests that I want to run with ctest. The tests can be executed by calling test in the directory /testing.

Any suggestions?

pipeline {

agent {label 'apitest-centos7'}
  stages {
    stage('cleanup'){
      steps{
        cleanWs()
      }
    }

    stage('git checkout Dockerfile') {
      steps{
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ebf01b-3gf93-4f8e-9f69-97ffgff308af', url: 'http://gitlab.somecompany.loc/somecompany/MyApiTestingTools.git']]])
        sh 'ls'
      }
    }

    stage('build Dockerimage'){
      steps{
        dir('dockerbuild'){
            fileExists 'Dockerfile'
            sh 'sudo docker build --no-cache=true -t apitestimage .'
        }
      }
    }

    stage('start Dockerimage and Tests') {
      steps{
        dir("dockerbuild"){
          withDockerContainer(args: "-ti apitestimage bash -c 'cd testing && ctest", image: 'apitestimage') {
          }              
        }
      }
    }
  }
  post {
    always {
          /*mattermostSend color: 'good', message: 'API-Tests have been executed'*/
          deleteDir() /* clean up our workspace */
    }
    success {
        mattermostSend color: 'good', message: 'API-Tests succeded', channel: 'api-tests'
    }
    unstable {
        mattermostSend color: 'warning', message: 'API-Tests are unstable', channel: 'api-tests'
    }
    failure {
        mattermostSend color: 'danger', message: 'API-Tests have failed', channel: 'api-tests', icon: 'https://talks.bitexpert.de/zendcon16-jenkins-for-php-projects/images/jenkins.png'
    }
    changed {
          mattermostSend color: 'warning', message: 'API-Tests have changed', channel: 'api-tests'
    }
  }
}

回答1:

I would guess 3 reasons for an error:

  • you're trying to launch the container interactive mode with tty (-it options) in a non-interactive without tty environment (the jenkins build environment), which may cause some problems
  • you're giving the image name twice: in args and image
  • you're not closing a quote

Try to change to this:

withDockerContainer(args: "bash -c 'cd testing && ctest'", image: 'apitestimage')

BTW, you may consider using the docker pipeline API:

stage('build Dockerimage') {
  steps {
    script {
      apitestimage = docker.build('apitestimage', '--no-cache=true dockerbuild')
    }
  }
}

stage('start Dockerimage and Tests') {
  steps{
    script {
      apitestimage.inside {
        sh 'cd testing && ctest'
      }
    }
  }
}

As the docker.inside mount a volume on the current workspace, you even could avoid the image creation step (you don't publish it, so I guess it's only necessary for tests) and launch the necessary commands from a base image to build the test environment and launch the tests.