简体   繁体   中英

Why does my docker build fail on jenkins agent?

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!

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'
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM