1
votes

Here is my Jenkinsfile pipeline in a project

pipeline {
    agent {
        docker {
            image 'docker:dind'
            args '-u root:root -p 3000:3000 --privileged'
        }
    }

    environment {
        CI = 'true'
    }

    stages {
        stage('docker build') {
            when {
                branch 'master'
            }
            steps {
                sh 'docker build --label v1.0.0 -t myrepo/myapp:v1.0.0'
            }
        }
    }
}

And I have a jenkins master and slave agent respectively. The above pipeline works well in master node, but if run in a slave agent node, then it would met the following error:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

I am pretty sure that docker is running on the agent node because I can ssh to it and run docker commands successfully.

Why it behaves differently between running on master and slave agent? How should I fix it? Thanks very much!

1
Sounds like a permission issue ? Login as user that jenkins uses and see if you are able to run docker commands on slave agent ? - ben5556
Notice the args "-u root:root" part, I have forced use the root user so it should not have permissions issue, right? - Jeff Tian
I was thinking that executes docker run command as root & it creates a container out of your image and runs docker daemon within your container as root user. Other users will be able to access it only using sudo or unless user is added to the docker group. I might be wrong here but that was my understanding.. - ben5556

1 Answers

1
votes

I don't know why but I fixed it with the following change: appened -v /var/run/docker.sock:/var/run/docker.sock to args.

pipeline {
    agent {
        docker {
            image 'docker:dind'
            args '-u root:root -p 3000:3000 --privileged -v /var/run/docker.sock:/var/run/docker.sock'
        }
    }

    environment {
        CI = 'true'
    }

    stages {
        stage('docker build') {
            when {
                branch 'master'
            }
            steps {
                sh 'docker build --label v1.0.0 -t myrepo/myapp:v1.0.0'
            }
        }
    }
}