Jenkins“执行脚本”构建步骤错误:/bin/docker:权限被拒绝。

4

我想在我的个人电脑上构建一个简单的流水线。我已经安装并运行了Jenkins ver. 2.32.2。以下是我流水线的作业DSL:

node {
    stage('Github Checkout') {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '921fd840-1d71-4b06-bf5e-1c6f3141f669', url: 'git@github.com:justinsr20/tdd_django.git']]])
    }
    stage('Build container image') {
        sh 'whoami'
    }
    stage('Build container image') {
        sh 'pwd'
    }
    stage('Build container image') {
        sh 'ls -l'
    }

    stage('Build container image') {
        sh 'docker build -t jenkins/django:v1 -t jenkins/django:latest .'
    }
}

我已经安装了Docker,也将jenkins用户添加到了docker组中:

[root@localhost bin]# grep 'docker' /etc/group
docker:x:977:judd,jenkins

奇怪的是,当我尝试在定义的流水线中运行docker构建命令时,jenkins给出了以下权限错误:
[workspace] Running shell script
+ whoami
jenkins
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build container image)
[Pipeline] sh
[workspace] Running shell script
+ pwd
/var/lib/jenkins/jobs/tdd_django/workspace
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build container image)
[Pipeline] sh
[workspace] Running shell script
+ ls -l
total 20
drwxr-xr-x. 3 jenkins jenkins 4096 Feb 10 11:11 django_app
-rw-r--r--. 1 jenkins jenkins   97 Feb 10 12:37 Dockerfile
-rw-r--r--. 1 jenkins jenkins   15 Feb 10 11:11 requirements.txt
-rw-r--r--. 1 jenkins jenkins   66 Feb 10 11:11 run_tests.sh
-rwxr-xr-x. 1 jenkins jenkins   69 Feb 10 12:48 script.sh
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build container image)
[Pipeline] sh
[workspace] Running shell script
+ docker build -t jenkins/django:v1 -t jenkins/django:latest .
/var/lib/jenkins/jobs/tdd_django/workspace@tmp/durable-77854956/script.sh: line 2: /bin/docker: Permission denied

我的Docker权限:

bash-4.3$ which docker
/bin/docker
bash-4.3$ ls -la /bin/docker
-rwxr-xr-x. 1 root root 12056512 Feb  8 16:44 /bin/docker

但是当我以Jenkins用户身份打开一个shell会话时,我可以轻松运行该命令:
[root@localhost bin]# sudo su -s /bin/bash jenkins
bash-4.3$ cd /var/lib/jenkins/jobs/tdd_django/workspace
bash-4.3$ ls
django_app  Dockerfile  requirements.txt  run_tests.sh  script.sh
bash-4.3$ docker build -t jenkins/django:v1 -t jenkins/django:latest .
Sending build context to Docker daemon  76.8 kB
Step 1/3 : FROM python:2.7-onbuild
# Executing 3 build triggers...
Step 1/1 : COPY requirements.txt /usr/src/app/
 ---> Using cache
Step 1/1 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Using cache
Step 1/1 : COPY . /usr/src/app
 ---> Using cache
 ---> fb9aa7078336
Step 2/3 : LABEL maintainer "CMI Industries"
 ---> Using cache
 ---> c1888038ffa2
Step 3/3 : RUN chmod +x /usr/src/app/run_tests.sh
 ---> Using cache
 ---> 8e61860e0567
Successfully built 8e61860e0567

任何帮助都将不胜感激。
编辑更新:
所以如果issues.jenkins-ci.org/browse/JENKINS-24338有关,我需要修改docker服务的启动方式。我使用以下方式启动docker服务:
systemctl start docker

我使用以下命令来查找此命令的配置文件:

[root@localhost system]# systemctl show --property=FragmentPath docker
FragmentPath=/usr/lib/systemd/system/docker.service

并且这个文件的内容是:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network.target firewalld.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target

我不确定如何修改这个文件,以便按照issues.jenkins-ci.org/browse/JENKINS-24338中所述,"配置docker服务将'jenkins'组所有权应用于套接字,而不是将jenkins用户添加到默认使用的'docker'组"。


如果你在sudo测试中使用sh而不是bash会怎样? - BMitch
[root@localhost bin]# sudo su -s /bin/sh jenkins sh-4.3$ type docker docker is /bin/docker - Justin S
你已经检查了所有明显的问题,应该可以工作。我看到目录列表上有一个selinux位设置,如果它没有被禁用,那么下一步我会尝试这个。 - BMitch
相关的吗?https://issues.jenkins-ci.org/browse/JENKINS-24338 - Nehal J Wani
嘿,Nehal,我认为你在那篇文章中可能有所发现。但我不确定如何修改Fedora的/usr/lib/systemd/system/docker.service文件。正如最后一条评论所提到的那样,它适用于CentOS,但是我在该文件中没有SocketGroup设置。我已经编辑了主要帖子。 - Justin S
请显示 script.sh 的内容。 - Alex O
1个回答

1
似乎你遇到了 selinux 问题。运行命令 sestatus 可以让你知道它的模式。在你的情况下,它是 enforcing
为了获得更多关于需要添加哪些规则才能使 selinux 正常的信息,我建议你通过编辑文件 /etc/sysconfig/selinux 将 selinux 设为 permissive,然后重新启动。我相信也有一种方法可以在不重启的情况下更改模式(虽然这不是永久性的)。
一旦 selinux 进入 permissive 模式(它只记录而不阻止),你可以在运行 jenkins 作业时尾随位于 /var/log/audit/audit.log 的日志。这应该能让你知道它需要哪些权限。

嘿,gbolo,谢谢你的回复。我尝试了建议的解决方案,但仍然收到相同的错误消息。 - Justin S
SELinux或者AppArmor是否已启用? - gbolo
嗨gbolo,我禁用了selinux,现在它成功运行了,谢谢 :) 我唯一的担忧是禁用selinux的后果是什么?我通过修改/etc/sysconfig/selinux并将seling更改为SELINUX=disbaled来禁用它。不确定是否有更细粒度的方法可以做到这一点? - Justin S
1
嗨,Justin,selinux有点超出了这个问题的范围。它基本上是一个内核模块,提供细粒度的访问控制。您需要添加规则以允许jenkins执行其所需的操作。我建议您设置SELINUX=permissive并在运行jenkins作业时观察selinux日志,以了解它需要什么样的访问权限。如果您发现此回答有用,请将其标记为已接受并点赞。 - gbolo
嘿,gbolo,当然我会看一下。你能编辑你的回答只谈论selinux吗?因为对我来说创建daemon.json文件行不通。那么我就很乐意点赞 :) - Justin S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接