dockerjenkinsjenkins-pipeline

sudo/docker not found while running the Jenkins pipeline


I have Jenkins running inside docker on an aws ec2 instance. I am using the following command to bring the Jenkins up:

sudo docker run --privileged --name jenkins-master -p 80:8080 -p 50000:50000 -v /var/jenkins_home:/var/jenkins_home -d jenkins/jenkins:lts

Following is my JenkinsFile:

pipeline {
    agent none
    stages {
        stage("Docker Permissions") {
        agent any
            steps {
                sh "sudo chmod 666 /var/run/docker.sock"
            }
        }
        stage('Build') {
            agent {
                docker {
                    image 'maven:3-alpine'
                    args '-v $HOME/.m2:/root/.m2'
                }
            }
            steps {
                sh 'mvn clean package -DskipTests'
            }
        }
        stage('Build') {
        agent none
            steps {
                script {
                    image = docker.build("image11")
                    println "Newly generated image: " + image.id
                }
            }
        }
    }
}

In the Jenkins job logs, I get sudo not found when I run the job. If I remove the first stage 'Docker Permissions' then I start getting following docker not found.

/var/jenkins_home/workspace test@tmp/durable-12345/script.sh: 1: /var/jenkins_home/workspace test@tmp/durable-12345/script.sh: docker: not found

Appreciate any help.


Solution

  • You don't need to change permissions during your first step.
    So you can remove your first stage Docker Permissions.

    Run your container like this :

    sudo docker run --name jenkins-master -p 80:8080 -p 50000:50000 -v /var/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker -d jenkins/jenkins:lts
    

    See informations on the docker forum.