一、Pipeline语句
Jenkinsfile (Declarative Pipeline) pipeline { agent {docker 'maven:3-alpine'} stages { stage('Example Build') { steps { sh 'mvn -v' } } } }
二、Pipeline执行结果
Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /var/lib/jenkins/workspace/pipeline-docker-job [Pipeline] { [Pipeline] sh + docker inspect -f . maven:3-alpine Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/maven:3-alpine/json: dial unix /var/run/docker.sock: connect: permission denied [Pipeline] sh + docker pull maven:3-alpine Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/images/create?fromImage=maven&tag=3-alpine: dial unix /var/run/docker.sock: connect: permission denied [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: script returned exit code 1 Finished: FAILURE
三、解决方法
出现“Got permission denied”报错,主要是权限问题,jenkins pipeline是在master上执行的,Jenkins是由jenkins用户启动的,但是jenkins用户没有执行docker的权限,所以导致以上报错,所以我们的解决方法如下:
# usermod -a -G docker jenkins # systemctl restart jenkins
仅仅将jenkins用户加入docker组还不够,必须要重启Jenkins才可以生效!