Mysql – Set up database for integration testing with Jenkins in Docker

continuous integrationdockergitJenkinsMySQL

Background:
I was following Downloading and running Jenkins in Docker to setup Jenkins Server. Used following parameter to run Docker.

docker run \
  -u root \
  --rm \  
  -d \ 
  -p 8080:8080 \ 
  -p 50000:50000 \ 
  -v jenkins-data:/var/jenkins_home \ 
  -v /var/run/docker.sock:/var/run/docker.sock \ 
  jenkinsci/blueocean

The build is running successfully, however, when it comes to Integration Test stage, a MySQL database is required.
The plan is Using multiple containers, while my Jenkinsfile is as following:

pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            when {
                changeRequest()
            }
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
        stage('Test') { 
            agent {
                docker {
                image 'mysql/mysql-server'
                args '--name some-mysql -e MYSQL_ROOT_PASSWORD=password -d'}
            }
            steps {
                sh 'mvn test -DforkCount=0'
                sh '''
                    docker exec some-mysql sh -c 'exec mysql < ./db/dump.sql
                    '''
            }
            post {
                always {
                    junit 'target/surefire-reports/*.xml' 
                }
            }
        }
    }
}

The ./db/dump.sql is in the same git repository with Jenkinsfile in db subdirectory.

Problem:

[workspace@2] Running shell script
+ docker inspect -f . mysql/mysql-server /var/jenkins_home/jobs/myproject/branches/master/workspace@2@tmp/durable-77d559d6/script.sh: line 1: docker: not found
[Pipeline] sh
[workspace@2] Running shell script
+ docker pull mysql/mysql-server /var/jenkins_home/jobs/myproject/branches/master/workspace@2@tmp/durable-21da0ff2/script.sh: ...   line 1: docker: not found
ERROR: script returned exit code 127
Finished: FAILURE

Since running as root, the privilege should not be the problem, right?
Using Docker with Pipeline provided several solutions, would other solution be much easier to set up the Integration Test environment?

Best Answer

The console output describes the error:

line 1: docker: not found

The shell script cannot find docker, likely because Docker is not installed on your Jenkins executor. (The Jenkins Docker plugin does not guarantee that Docker will be available to Jenkins-external processes, such as shell scripts.)

Related Topic