3

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?

jayhendren
  • 1,014
  • 5
  • 12
Shihe Zhang
  • 143
  • 1
  • 7

2 Answers2

1

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.)

jayhendren
  • 1,014
  • 5
  • 12
0

Docker client is not installed on the container. Either install docker or use a image of jenkins + docker. Also docker daemon of host machine is used and need to mount it.

Below commands should help:

  1. docker volume create jenkins-master-data

  2. docker run --name jenkins-master -u 0 --privileged -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v jenkins-master-data:/var/jenkins_home gustavoapolinario/jenkins-docker

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Sanket
  • 101
  • 1