miserable
miserable

Reputation: 727

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.

Upvotes: 2

Views: 5342

Answers (2)

fmdaboville
fmdaboville

Reputation: 1831

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
  • You can remove the --privileged flag
  • You need to share the docker host with your container :
    -v /var/run/docker.sock:/var/run/docker.sock
  • You also need to share the docker path to your container :
    -v $(which docker):/usr/bin/docker

See informations on the docker forum.

Upvotes: 2

Vinod
Vinod

Reputation: 533

In Docker it is not recommended to use "sudo". If you are able to run docker then you can very easily become root. while doing Jenkins up you are already using --privileged which should run this command with higher privilege.

Try to remove sudo from command and run.

Upvotes: -2

Related Questions