Jenkins 执行Docker build错误:Got permission denied while trying to connect to the Docker daemon...

目录

 

背景

解决办法


背景

最近使用Jenkins + Docker做自动化部署,由于安全原因没有使用mvn + docker plugin生成并推送镜像(这种方式会暴露2375端口漏洞,但可以自己使用证书解决),我采用的是在Jenkins执行shell脚本:

docker build -t freeshop/freeshop-sms .

发现报错:

+ docker build -t xxx/xxx-user .
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.26/build?buildargs=%7B%7D&buildbinds=null&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=xxx%2Fxxx-user&ulimits=null: dial unix /var/run/docker.sock: connect: permission denied
Build step 'Execute shell' marked build as failure
Finished: FAILURE

解决办法

之前百度了很多文章都是添加用户组,但并没有用。然后直接google了一下,终于得到可以解决的方法:

把jenkins用户,加到docker用户组

如果没有docker用户组,先创建用户组:

groupadd docker

添加jenkins用户到用户组:

sudo usermod -a -G docker jenkins

  • -a<追加> 必须与-G选项一起使用,把用户追加到某些组中。
  • -G<群组> 修改用户所属的附加群组。

发现报错还存在,继续修改/var/run/docker.sock文件的权限

cd /var/run

chmod 777 docker.sock

发现问题终于解决。 亲测有效。

猜你喜欢

转载自blog.csdn.net/qq_26878363/article/details/110479644