繁体   English   中英

无法连接到 jenkins docker 容器内的 docker 容器 MacOS

[英]Can't connect to docker inside jenkins docker container MacOS

经过整整两天的阅读和尝试,我谦卑地来到这里询问如何使这项工作发挥作用,因为其他答案中的任何内容都没有帮助我完成这项工作。

我在 macos 10.13.6 (High Sierra)

运行 Docker Desktop for mac 2.2.0.5 (43884)

Engine: 19.03.8
Compose 1.25.4

我想运行 jenkins 来研究一些管道的东西,所以这是我的“docker-compose.yml”

version: "3.2"

services:
  jenkins:

    build: 
      dockerfile: dockerfile
      context: ./build

    ports:
      - "8080:8080"
      - "50000:50000"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./data:/var/jenkins_home

First problem is that the image that i'm using jenkins/jenkins:lts does not have a docker client installed, so even mapping the socket through volumes I can't use docker version the output of this command is bash: docker: command not found

这是我仅用于测试的管道(来自 jenkins 文档):

pipeline {
    agent { docker { image 'node:6.3' } }
    stages {
        stage('build') {
            steps {
                sh 'npm --version'
            }
        }
    }
}

So through this plugin https://plugins.jenkins.io/docker-plugin/ I can go to "Manage Jenkins > Manage Nodes and Clouds > Configure Clouds > Add a new cloud" and on "Docker Cloud details..."

我有主机 URI,我可以在其中放置“unix:///var/run/docker.sock”,它将使用我的主机 macos 中的 docker 来执行 jenkins 需要做的事情。

I tried all the suggestion from the internet, from create the jenkins user, docker user, put jenkins user on docker group e other stuff but none of them work on the mac.

大部分被问到的问题是针对 linux 的,它们似乎都解决了问题,但是当我尝试在 macos 上进行复制时,它就不起作用了。

也许我错过了一些步骤,或者人们已经知道他们必须在某些步骤中做,但我失败得很惨。

我尝试的一些步骤:

创建使用用户和组 jenkins:

sudo dscl . create /Users/jenkins UniqueID 1000 PrimaryGroupID 1000
sudo dscl . create /Groups/jenkins gid 1000

创建组 docker:

sudo dscl . create /Groups/docker gid 1001

将 jenkins 用户添加到 docker 组

sudo dscl . append /Groups/docker GroupMembership jenkins

检查用户是否真的在组中

$ dsmemberutil checkmembership -u 1000 -g 1001
user is a member of the group

试图从 jenkins 容器内部更改套接字的所有者(这就是我构建映像的原因,但它不起作用)

试图更改主机 macos 上套接字的所有权,但它只是没有改变。 套接字始终具有这些权限。

lrwxr-xr-x 1 root daemon 68B Apr 28 10:14 docker.sock -> /Users/metasix/Library/Containers/com.docker.docker/Data/docker.sock

对于 jenkins,最好的方法是拥有将运行所有作业的代理和只执行编排作业的主服务器。

几年前,我构建了一个注册到 jenkins master 的 JNLP 代理,你可以在这里查看我的仓库: https://github.com/jmaitrehenry/docker-jenkins-jnlp正如我所说,我做到了就像 3 年前一样可能有点过时了。

关于您的问题,您需要知道 Docker for Mac 在一个小 VM 内运行容器,因此当您在 MacOS 上添加用户时,VM 没有它。 和 Docker for Mac 对 Mac 内的 map uid 和容器内的一些 uid 做了很多魔术。

您可以尝试在 Dockerfile 中添加 docker 客户端,为此,请尝试添加以下步骤:

FROM jenkins/jenkins:lts
[...]

# Switch to root as the base image switch to jenkins user
USER root

# Download docker-cli and install it
RUN curl -o docker-ce-cli.deb https://download.docker.com/linux/debian/dists/stretch/pool/stable/amd64/docker-ce-cli_19.03.8~3-0~debian-stretch_amd64.deb && \
    dpkg -i docker-ce-cli.deb && \
    rm docker-ce-cli.deb

# Switch back to jenkins user
USER jenkins

您需要通过将network: host添加到您的撰写文件来启用主机模式网络:


services:
  jenkins:

    build: 
      dockerfile: dockerfile
      context: ./build
      network: host

    ports:
      - "8080:8080"
      - "50000:50000"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./data:/var/jenkins_home

这将允许您的访客 docker 容器查看主机网络。 问题是 Docker Desktop for MacOS 不支持通过 TCP 端口进行监听。 使用 socat 有一个已知的解决方法。 https://www.ivankrizsan.se/2016/05/21/docker-api-over-http-on-mac-os-x-with-docker-for-mac-beta/ 将 socat 设置为从 docker.socker 路由到 TCP 2376 后,将主机 URI 设置为 ZE20BB202B1D5537B1415E3263A37ED738Z:://0.0.0。 当然,您需要创建一个新的 Dockerfile 以使用FROM jenkins/jenkins:lts lts 扩展 jenkins/jenkins:lts 并按照另一个答案中的建议将 Docker 添加到容器中

我遇到了同样的问题。 即使将用户添加到 docker 组后, jenkins用户也无法运行 docker 命令。

当我检查主机(MacOS)上的权限时, docker.sock文件归root:daemon所有。

ls -lart /var/run/docker.sock
lrwxr-x--x  1 root  daemon  37 Feb  1 14:56 /var/run/docker.sock -> /Users/....

我将权限更新为755并开始工作。 我能够以jenkins用户身份在容器上运行 docker 命令。

请仅更改开发环境的主机文件权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM