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

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

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

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

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


当前回答

在詹金斯运行的服务器上,我用了

sudo setfacl -m user:tomcat:rw /var/run/docker.sock

然后运行每个docker容器

-v /var/run/docker.sock:/var/run/docker.sock

使用setfacl似乎是更好的选择,并且不需要“-u user”。然后,容器以运行Jenkins的同一用户运行。但我很感激安全专家的反馈。

其他回答

如果使用jenkins

用户jenkins需要添加到docker组中:

sudo usermod -a -G docker jenkins

然后重启詹金斯。

否则

如果你收到来自docker的消息而遇到堆栈溢出的问题,但你没有使用jenkins,那么错误很可能是一样的:你的非特权用户不属于docker组。

你可以:

sudo usermod -a -G docker [user]

在[user]所在的位置插入用户名。

你可以通过执行grep docker /etc/group命令来检查它是否成功,如下所示:

docker:x:998:[user]

在其中一行。

然后将您的用户组ID更改为docker(以避免不得不退出并再次登录):

newgrp docker

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

希望这能有所帮助

我们有两种方法来解决这个问题

方法1 应用新的组成员资格

sudo usermod -aG docker ${USER}
su - ${USER}

方法2 修改文件权限和组权限

chmod 777 /var/run/docker.sock
sudo chown root:docker /var/run/docker.sock

方法1为安全方法。

2019-05-26

这对我很管用!

示例docker-compose:

version: "3"
services:
  jenkins:
    image: jenkinsci/blueocean
    privileged: true
    ports:
      - "8080:8080"
    volumes:
      - $HOME/learning/jenkins/jenkins_home:/var/jenkins_home
    environment:
      - DOCKER_HOST=tcp://socat:2375
    links:
      - socat

  socat:
     image: bpack/socat
     command: TCP4-LISTEN:2375,fork,reuseaddr UNIX-CONNECT:/var/run/docker.sock
     volumes:
        - /var/run/docker.sock:/var/run/docker.sock
     expose:
        - "2375"

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

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