如何在作为容器的 Jenkins 节点中获得 docker 运行?

How can I get docker running in Jenkins nodes which are containers?

我正在尝试在 Jenkins 上获取 docker 运行,它本身就是一个容器。以下是 Pod 规范的一部分。

cyrilpanicker/jenkins 是安装了 Jenkins 和 docker-cli 的镜像。 对于 Docker 守护进程,我是 运行 另一个具有 docker:dind 图像的容器(节点是 运行 在 k8s 集群上)。 为了在它们之间建立 docker.sock 链接,我使用了卷挂载。

spec:
  containers:
    - name: jenkins
      image: cyrilpanicker/jenkins
      volumeMounts:
        - mountPath: /var/run/docker.sock
          name: docker-socket
    - name: docker
      image: docker:dind
      securityContext:
        privileged: true
      volumeMounts:
        - mountPath: /var/run/docker.sock
          name: docker-socket
  volumes:
    - name: docker-socket
      hostPath:
        path: /docker.sock
        type: FileOrCreate

但这不起作用。以下是来自 docker 容器的日志。

time="2021-06-04T20:47:26.059792967Z" level=info msg="Starting up"
time="2021-06-04T20:47:26.061956820Z" level=warning msg="could not change group /var/run/docker.sock to docker: group docker not found"
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy

任何人都可以建议另一种方法来让它工作吗?

根据 kubernetes 文档,hostPath 从节点文件系统挂载路径,所以如果我理解正确的话,这不是你想要实现的。 恐怕无法将单个文件挂载为一个卷,因此即使您从 volumes 中删除 hostPathdocker.sock 也会挂载为目录:

jenkins@static-web:/$ ls -la /var/run/
total 20
drwxr-xr-x 1 root root 4096 Jun  5 14:44 .
drwxr-xr-x 1 root root 4096 Jun  5 14:44 ..
drwxrwxrwx 2 root root 4096 Jun  5 14:44 docker.sock

我会尝试 运行 docker dind 容器中的守护进程,使用 TCP 侦听器而不是 sock 文件:

spec:
  containers:
    - name: jenkins
      image: cyrilpanicker/jenkins
    - name: docker
      image: docker:dind
      command: ["dockerd"]
      args: ["-H", "tcp://127.0.0.1:2376"]
      ports:
        - containerPort: 2376
      securityContext:
        privileged: true
jenkins@static-web:/$ docker -H tcp://127.0.0.1:2376 ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

然后配置 jenkins 使用 tcp://127.0.0.1:2376 作为远程 docker 守护进程。