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

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

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

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

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


当前回答

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

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 setfacl -m user:tomcat:rw /var/run/docker.sock

然后运行每个docker容器

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

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

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

希望这能有所帮助

这在Ubuntu 20.04中适用

sudo chmod 666 /var/run/docker.sock

不知道它到底能做什么,但能解决问题。

使用下面的dockerfile

FROM jenkins/jenkins

USER root

# Install Docker
RUN apt-get update && \
    apt-get -y install apt-transport-https \
    ca-certificates \
    curl \
    gnupg2 \
    software-properties-common && \
    curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
    add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
    $(lsb_release -cs) \
    stable" && \
    apt-get update && \
    apt-get -y install docker-ce


# Compose
RUN curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose



RUN usermod -aG docker jenkins
RUN usermod -aG root jenkins

USER jenkins

我使用的是官方的jenkins docker映像(https://hub.docker.com/r/jenkins/jenkins),但我认为这个解决方案适用于我们想要在docker容器中运行docker的大多数用例。

在Docker容器中使用Docker的推荐方法是使用主机系统的Docker守护进程。这方面的好文章:https://itnext.io/docker-in-docker-521958d34efd。

处理权限问题的秘诀是在容器内为容器的用户添加权限,而不是为主机系统添加权限。默认情况下只有root用户有权限这样做,所以

docker exec -it -u root <container-name> bash
usermod -a -G docker <username>

我会做的。记得重新启动容器。

我想最简单的方法是创建一个定制的Dockerfile:

# Official jenkins image
FROM jenkins/jenkins:lts
# Swith to root to be able to install Docker and modify permissions
USER root
RUN apt-get update
# Install docker
RUN curl -sSL https://get.docker.com/ | sh
# Add jenkins user to docker group
RUN usermod -a -G docker jenkins
# Switch back to default user
USER jenkins

# Bild the image:
# sudo docker build -t yourusername/imagename .
# Run the image and mount with the followin bind mount option:
# sudo docker run --name imagename -d -p8080:8080 -v /var/run/docker.sock:/var/run/docker.sock yourusername/imagename