Jenkins拨打Unix /var/run/docker.sock:连接:权限被MacOS拒绝

时间:2019-06-30 20:42:07

标签: macos docker jenkins

我在本地运行Jenkins,并且在我的机器上也安装了docker。

我有一个使用Jenkins文件的詹金斯工作

  1. 构建一个Maven项目
  2. 构建docker映像
  3. 部署到docker hub。

我在Jenkins中安装了所有docker插件,但是当执行Build步骤时,我得到了...

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.39/build?
buildargs=%7B%7D&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&session=w6ypm3t1b0qefyxh9omfvntru&shmsize=0&t=app-web&target=&ulimits=null&version=1: dial unix /var/run/docker.sock: connect: permission denied

我似乎无法更改

的权限
lrwxr-xr-x   1 macuser          staff              72 Jun 30 20:36 docker.sock -> /Users/john/Library/Containers/com.docker.docker/Data/docker.sock

任何帮助表示赞赏

2 个答案:

答案 0 :(得分:2)

您的主要问题,因为通过Jenkins使用的用户(可能名为jenkins)没有足够的权限来运行docker。

因此,您基本上需要使用户加入staff组。

几天前我有answered个问题,答案的后半部分是您要寻找的内容:How to add user to a group from Mac OS X command line?

请对其进行测试,并在尝试将用户添加到staff组时获得的输出结果告诉我,它是否对您不起作用

答案 1 :(得分:1)

对于Mac的Docker,在容器内部,您会发现Docker套接字归root所有(这是嵌入式LinuxKit VM的一部分)。我在Jenkins容器内使用以下入口点,并以root身份运行入口点,以自动重新配置容器内的docker组以匹配套接字文件的组ID,然后在运行Jenkins之前从root放到jenkins用户应用程序本身。这样做的优点是可移植,可以在任何桌面或服务器环境上运行,而无需将Docker GID硬编码到容器中:

#!/bin/sh

# By: Brandon Mitchell <public@bmitch.net>
# License: MIT
# Source Repo: https://github.com/sudo-bmitch/jenkins-docker

set -x

# configure script to call original entrypoint
set -- tini -- /usr/local/bin/jenkins.sh "$@"

# In Prod, this may be configured with a GID already matching the container
# allowing the container to be run directly as Jenkins. In Dev, or on unknown
# environments, run the container as root to automatically correct docker
# group in container to match the docker.sock GID mounted from the host.
if [ "$(id -u)" = "0" ]; then
  # get gid of docker socket file
  SOCK_DOCKER_GID=`ls -ng /var/run/docker.sock | cut -f3 -d' '`

  # get group of docker inside container
  CUR_DOCKER_GID=`getent group docker | cut -f3 -d: || true`

  # if they don't match, adjust
  if [ ! -z "$SOCK_DOCKER_GID" -a "$SOCK_DOCKER_GID" != "$CUR_DOCKER_GID" ]; then
    groupmod -g ${SOCK_DOCKER_GID} -o docker
  fi
  if ! groups jenkins | grep -q docker; then
    usermod -aG docker jenkins
  fi
  # Add call to gosu to drop from root user to jenkins user
  # when running original entrypoint
  set -- gosu jenkins "$@"
fi

# replace the current pid 1 with original entrypoint
exec "$@"

您可以在以下位置找到完整的示例,包括用于在映像内安装docker和gosu的Dockerfile:

我的基本映像中的fix-perms脚本中有相同的概念,该脚本可以应用于其他场景:https://github.com/sudo-bmitch/jenkins-docker