无法在docker内部运行systemd,该程序正在jenkins内部运行

无法在docker内部运行systemd,该程序正在jenkins内部运行,docker,jenkins,systemd,Docker,Jenkins,Systemd,我正试图让詹金斯管理运行SystemD的Docker 到目前为止,我已经能够在没有Jenkins的情况下在docker本地运行systemd。以下是在没有jenkins的情况下在本地运行它的步骤: # pull unop/fedora-systemd and create and run the container for it sudo docker run --cap-add=SYS_ADMIN -e container=docker --tmpfs /run --tmpfs /tmp -v

我正试图让詹金斯管理运行SystemD的Docker

到目前为止,我已经能够在没有Jenkins的情况下在docker本地运行systemd。以下是在没有jenkins的情况下在本地运行它的步骤:

# pull unop/fedora-systemd and create and run the container for it
sudo docker run --cap-add=SYS_ADMIN -e container=docker --tmpfs /run --tmpfs /tmp -v /sys/fs/cgroup:/sys/fs/cgroup:ro -t -i unop/fedora-systemd

# on a different terminal window, I can:
# get the container id of the "unop/fedora-systemd" image
sudo docker ps

# then exec bash on it
sudo docker container exec -t -i a98aa2bcd19e bash # where a98aa2bcd19e is the container id found above

# once inside the container, I can run systemd without any problems. examples:
systemctl status
systemctl start dbus.service
systemctl status dbus.service
上述操作在本地运行,我可以在docker容器中运行systemd。 我遇到的问题是当我尝试同样的事情时,但是在詹金斯的内心

我已经多次尝试调整Jenkinsfile,但之前的尝试似乎都不起作用。在Jenkins下运行时,我总是遇到一个类似以下情况的错误:

+ systemctl status
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
这是我最新尝试的詹金斯文件

pipeline {
    agent {
        docker {
            image 'unop/fedora-systemd'
            args '--cap-add=SYS_ADMIN -e container=docker --tmpfs /run --tmpfs /tmp -v /sys/fs/cgroup:/sys/fs/cgroup:ro -t -i'
        }
    }

    stages {
        stage('test') {
            steps {
                sh "echo hello world"
                sh "systemctl status"
                sh "systemctl start dbus.service"
                sh "systemctl dbus.service"
            }
        }
    }
}
在Jenkins文件的上一次迭代中,我尝试将
-cap add=SYS\u ADMIN-e container=docker
替换为
--privileged
,但这没有帮助,我仍然得到了相同的错误

有人知道我该如何让它工作吗?为什么上述方法在本地有效,但在詹金斯身上却不起作用?我错过了什么?

注:Jenkins版本:2.150.2,这是unop/fedora systemd使用的Dockerfile


附言:我看过一篇文章,但他们问的是不同的

我不知道相关的问题。让我再次指出,如果systemd控制的容器中只运行多个服务,则不需要在其中运行systemd守护程序。只需用脚本覆盖
/usr/bin/systemctl
。然后使用
CMD[“/usr/bin/systemctl”]
将其注册为容器的初始化进程


就这样。现在,您可以从操作系统运行任何systemctl启动进程。它的工作原理是,即使使用ansible/puppet脚本进行资源调配也没有任何问题。特别是,我用它为Jenkins映像提供开发人员喜欢的操作系统作为基础。不需要特权模式。

您可以使用此命令尝试已激活系统D的Fedora图像:

docker run-d--name systemd fedora--privileged-v/sys/fs/cgroup:/sys/fs/cgroup:ro-jrei/systemd fedora

然后,您只需运行:

docker exec-it系统fedora/bin/bash


在那里,您只需安装、启动和重新启动所需的任何服务。

以下文章中的信息可能很有用:。这是前一篇文章的后续文章,该文章最初提出了一些类似于您的
unop/fedora systemd
代码片段的东西。
FROM fedora:rawhide
MAINTAINER http://fedoraproject.org/wiki/Cloud

ENV container docker

RUN dnf -y update && dnf clean all

RUN dnf -y install systemd && dnf clean all && \
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;

VOLUME [ "/sys/fs/cgroup", "/tmp", "/run" ]
CMD ["/usr/sbin/init"]