0

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?

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

  • So what would be your suggestion? – Shihe Zhang Jun 30 '18 at 1:11
  • Install Docker on your Jenkins executor – jayhendren Jun 30 '18 at 5:28
  • Is agent docker part able to handle it? Feel install docker in docker is not a good idea in instinct. – Shihe Zhang Jun 30 '18 at 6:42
  • I plan to write a Dockfile build FROM MySQL and Jenkins would that works? – Shihe Zhang Jul 3 '18 at 1:29
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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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