Answer a question

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.

Answers

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.

Logo

CI/CD社区为您提供最前沿的新闻资讯和知识内容

更多推荐