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

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

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

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

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


当前回答

简单地添加docker作为jenkins用户的补充组

sudo usermod -a -G docker jenkins

在使用Docker映像作为Jenkins代理时并不总是足够的。也就是说,如果你的Jenkinsfile以pipeline{agent{dockerfile或pipeline{agent{image开头:

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.jenkinsAgent'
        }
    }
    stages {

这是因为Jenkins执行了一个docker运行命令,这会导致三个问题。

代理将(可能)没有安装Docker程序。 Agent将无法访问Docker守护进程套接字,因此将尝试运行Docker-in-Docker,这是不推荐的。 Jenkins给出代理应该使用的数字用户ID和数字组ID。Agent不会有任何补充组,因为docker run不会登录容器(它更像sudo)。

安装Agent的Docker

让Docker程序在Docker镜像中可用只需要在Dockerfile中运行Docker安装步骤:

# Dockerfile.jenkinsAgent
FROM debian:stretch-backports
# Install Docker in the image, which adds a docker group
RUN apt-get -y update && \
 apt-get -y install \
   apt-transport-https \
   ca-certificates \
   curl \
   gnupg \
   lsb-release \
   software-properties-common

RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

RUN apt-get -y update && \
 apt-get -y install \
   docker-ce \
   docker-ce-cli \
   containerd.io

...

共享Docker守护进程socket

如前所述,解决第二个问题意味着运行Jenkins Docker容器,以便它与容器外部的Docker守护进程共享Docker守护进程套接字。所以你需要告诉Jenkins使用共享运行Docker容器,如下所示:

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.jenkinsAgent'
            args '-v /var/run/docker.sock:/var/run/docker.sock'
        }
    }

设置uid和gid

The ideal fix to the third problem would be set up supplementary groups for the Agent. That does not seem possible. The only fix I'm aware of is to run the Agent with the Jenkins UID and the Docker GID (the socket has group write permission and is owned by root.docker). But in general, you do not know what those IDs are (they were allocated when the useradd ... jenkins and groupadd ... docker ran when Jenkins and Docker were installed on the host). And you can not simply tell Jenkins to user user jenkins and group docker

args '-v /var/run/docker.sock:/var/run/docker.sock -u jenkins:docker'

因为这告诉Docker在镜像中使用名为jenkins和Docker的用户和组,而你的Docker镜像可能没有jenkins用户和组,即使它有,也不能保证它与主机有相同的UID和GID,同样也不能保证Docker的GID是相同的

幸运的是,Jenkins在脚本中为Dockerfile运行docker build命令,所以你可以使用一些shell脚本魔法来将这些信息作为docker build参数传递:

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.jenkinsAgent'
            additionalBuildArgs  '--build-arg JENKINSUID=`id -u jenkins` --build-arg JENKINSGID=`id -g jenkins` --build-arg DOCKERGID=`stat -c %g /var/run/docker.sock`'
            args '-v /var/run/docker.sock:/var/run/docker.sock -u jenkins:docker'
        }
    }

它使用id命令获取jenkins用户的UID和GID,并使用stat命令获取关于Docker套接字的信息。

你的Dockerfile可以使用这些信息为Agent设置一个jenkins用户和docker组,使用groupadd, groupmod和useradd:

# Dockerfile.jenkinsAgent
FROM debian:stretch-backports
ARG JENKINSUID
ARG JENKINSGID
ARG DOCKERGID
...
# Install Docker in the image, which adds a docker group
RUN apt-get -y update && \
 apt-get -y install \
   apt-transport-https \
   ca-certificates \
   curl \
   gnupg \
   lsb-release \
   software-properties-common

RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

RUN apt-get -y update && \
 apt-get -y install \
   docker-ce \
   docker-ce-cli \
   containerd.io

...
# Setup users and groups
RUN groupadd -g ${JENKINSGID} jenkins
RUN groupmod -g ${DOCKERGID} docker
RUN useradd -c "Jenkins user" -g ${JENKINSGID} -G ${DOCKERGID} -M -N -u ${JENKINSUID} jenkins

其他回答

我有Jenkins在Docker中运行,并连接Jenkins从主机Ubuntu 16.04通过卷到/var/run/docker.sock使用Docker套接字。

对我来说,解决方案是:

1) Jenkins的Docker容器内部(Docker exec - Jenkins bash on host machine)

usermod -a -G docker jenkins
chmod 664 /var/run/docker.sock
service jenkins restart (or systemctl restart jenkins.service)
su jenkins

2)主机上:

sudo service docker restart

664表示-对组中的所有者和用户进行读写(但不执行)。

修改docker的访问权限。袜子文件

chmod 777 /var/run/docker.sock

也可以在命令的开头使用sudo。

Chmod 777将允许所有用户的所有操作,而Chmod 666将允许所有用户读和写,但不能执行文件。

改变波纹""your_user"为真正的用户

echo "your_user ALL=(ALL) NOPASSWD: /usr/local/bin/docker, /usr/local/sbin/docker, /usr/bin/docker, /usr/sbin/docker, /bin/docker, /sbin/docker" > /etc/sudoers.d/sudo_docker

usermod -aG docker your_user

chmod 0660 /var/run/docker.sock

chown root:docker /var/run/docker.sock

如果使用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

简单地添加docker作为jenkins用户的补充组

sudo usermod -a -G docker jenkins

在使用Docker映像作为Jenkins代理时并不总是足够的。也就是说,如果你的Jenkinsfile以pipeline{agent{dockerfile或pipeline{agent{image开头:

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.jenkinsAgent'
        }
    }
    stages {

这是因为Jenkins执行了一个docker运行命令,这会导致三个问题。

代理将(可能)没有安装Docker程序。 Agent将无法访问Docker守护进程套接字,因此将尝试运行Docker-in-Docker,这是不推荐的。 Jenkins给出代理应该使用的数字用户ID和数字组ID。Agent不会有任何补充组,因为docker run不会登录容器(它更像sudo)。

安装Agent的Docker

让Docker程序在Docker镜像中可用只需要在Dockerfile中运行Docker安装步骤:

# Dockerfile.jenkinsAgent
FROM debian:stretch-backports
# Install Docker in the image, which adds a docker group
RUN apt-get -y update && \
 apt-get -y install \
   apt-transport-https \
   ca-certificates \
   curl \
   gnupg \
   lsb-release \
   software-properties-common

RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

RUN apt-get -y update && \
 apt-get -y install \
   docker-ce \
   docker-ce-cli \
   containerd.io

...

共享Docker守护进程socket

如前所述,解决第二个问题意味着运行Jenkins Docker容器,以便它与容器外部的Docker守护进程共享Docker守护进程套接字。所以你需要告诉Jenkins使用共享运行Docker容器,如下所示:

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.jenkinsAgent'
            args '-v /var/run/docker.sock:/var/run/docker.sock'
        }
    }

设置uid和gid

The ideal fix to the third problem would be set up supplementary groups for the Agent. That does not seem possible. The only fix I'm aware of is to run the Agent with the Jenkins UID and the Docker GID (the socket has group write permission and is owned by root.docker). But in general, you do not know what those IDs are (they were allocated when the useradd ... jenkins and groupadd ... docker ran when Jenkins and Docker were installed on the host). And you can not simply tell Jenkins to user user jenkins and group docker

args '-v /var/run/docker.sock:/var/run/docker.sock -u jenkins:docker'

因为这告诉Docker在镜像中使用名为jenkins和Docker的用户和组,而你的Docker镜像可能没有jenkins用户和组,即使它有,也不能保证它与主机有相同的UID和GID,同样也不能保证Docker的GID是相同的

幸运的是,Jenkins在脚本中为Dockerfile运行docker build命令,所以你可以使用一些shell脚本魔法来将这些信息作为docker build参数传递:

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.jenkinsAgent'
            additionalBuildArgs  '--build-arg JENKINSUID=`id -u jenkins` --build-arg JENKINSGID=`id -g jenkins` --build-arg DOCKERGID=`stat -c %g /var/run/docker.sock`'
            args '-v /var/run/docker.sock:/var/run/docker.sock -u jenkins:docker'
        }
    }

它使用id命令获取jenkins用户的UID和GID,并使用stat命令获取关于Docker套接字的信息。

你的Dockerfile可以使用这些信息为Agent设置一个jenkins用户和docker组,使用groupadd, groupmod和useradd:

# Dockerfile.jenkinsAgent
FROM debian:stretch-backports
ARG JENKINSUID
ARG JENKINSGID
ARG DOCKERGID
...
# Install Docker in the image, which adds a docker group
RUN apt-get -y update && \
 apt-get -y install \
   apt-transport-https \
   ca-certificates \
   curl \
   gnupg \
   lsb-release \
   software-properties-common

RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

RUN apt-get -y update && \
 apt-get -y install \
   docker-ce \
   docker-ce-cli \
   containerd.io

...
# Setup users and groups
RUN groupadd -g ${JENKINSGID} jenkins
RUN groupmod -g ${DOCKERGID} docker
RUN useradd -c "Jenkins user" -g ${JENKINSGID} -G ${DOCKERGID} -M -N -u ${JENKINSUID} jenkins