我是码头工人的新手。我只是试着在我的本地机器(Ubuntu 16.04)上使用docker和Jenkins。

我用下面的管道脚本配置了一个新作业。

node {
    stage('Build') {
      docker.image('maven:3.3.3').inside {
        sh 'mvn --version'
      }
    }
}

但是它失败了,错误如下:

在unix:///var/run/ Docker .sock试图连接到Docker守护进程套接字时被拒绝


当前回答

在我的例子中,不仅需要将jenkins用户添加到docker组,而且需要将该组作为jenkins用户的主组。

# usermod -g docker jenkins
# usermod -a -G jenkins jenkins

不要忘记重新连接jenkins从节点或重新启动jenkins服务器,这取决于您的情况。

其他回答

sudo setfacl——modify user:(用户名或ID):rw /var/run/docker.sock

我试着执行了几次命令

Sudo chmod 777 /var/run/docker.sock

但不幸的是,我每次登录ubuntu系统时都要这么做。 它不需要重新启动,比usermod或chown更安全。 如果用户名只存在于容器内,而不存在于主机上,则需要输入user ID。

我希望它能帮助你解决这个问题。

如果你可能会得到如下错误,

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

or

level=error msg="failed to dial gRPC: cannot connect to the Docker daemon. Is 'docker daemon' running on this host?: dial unix /var/run/docker.sock: connect: permission denied"

试着执行下面的命令,

$ sudo su - jenkins
$ sudo usermod -a -G docker $USER
$ sudo chown jenkins:docker /var/run/docker.sock

我的成功

sudo usermod -a -G docker $USER
reboot

在我的情况下,这将成功工作。 导航本地回购并输入此命令。

sudo chmod 666 /var/run/docker.sock

2018-08-19

我已经被这个问题困了好几天了,因为我还没有找到一个完整的答案,关于为什么和如何,我将为其他偶然发现同样问题的人发布一个答案,上面的答案不起作用。

以下是在docker中运行Jenkins时的3个关键步骤:

You mount the socket /var/run/docker.sock to the jenkins container in order to be able to use the docker from the host. You have to install docker inside the container in order to use it. This is a great and simple article on how to do that. Note that newer versions might already have docker installed You run sudo usermod -a -G docker jenkins in order to add jenkins to the docker group. However, here you might run into a permission problem if the host docker and the container docker don't have the same group id so it is very important to adjust the container docker's gid to be the same as the host docker gid

您可以将此作为启动脚本的一部分,也可以使用exec并手动执行:groupmod -g <YOUR_HOST_DOCKER_GID> docker。

另外,不要更改/var/run/docker的权限。Sock到777或类似的东西,因为这是一个很大的安全风险,你基本上允许每个人在你的机器上使用docker

希望这能有所帮助