Set up database for integration testing with Jenkins in Docker

6,757

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

Share:
6,757

Related videos on Youtube

Shihe Zhang
Author by

Shihe Zhang

Updated on September 18, 2022

Comments

  • Shihe Zhang
    Shihe Zhang over 1 year

    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?

  • Shihe Zhang
    Shihe Zhang almost 6 years
    So what would be your suggestion?
  • jayhendren
    jayhendren almost 6 years
    Install Docker on your Jenkins executor
  • Shihe Zhang
    Shihe Zhang almost 6 years
    Is agent docker part able to handle it? Feel install docker in docker is not a good idea in instinct.
  • Shihe Zhang
    Shihe Zhang almost 6 years
    I plan to write a Dockfile build FROM MySQL and Jenkins would that works?