简体   繁体   English

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

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

After two full days reading and trying thing, I humbling come here to ask how to make this work, because nothing from the other answers helped me to make this work.经过整整两天的阅读和尝试,我谦卑地来到这里询问如何使这项工作发挥作用,因为其他答案中的任何内容都没有帮助我完成这项工作。

I'm on a macos 10.13.6 (High Sierra)我在 macos 10.13.6 (High Sierra)

Running Docker Desktop for mac 2.2.0.5 (43884)运行 Docker Desktop for mac 2.2.0.5 (43884)

Engine: 19.03.8
Compose 1.25.4

I want to run jenkins to study some pipeline stuff, so this is my ´docker-compose.yml´我想运行 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 . 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

This is my pipeline just for test (from jenkins documentation):这是我仅用于测试的管道(来自 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..." 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..."

I have the Host URI where I can put "unix:///var/run/docker.sock" that it will use the docker from my host macos to do what jenkins need to do.我有主机 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. 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.

The big majority of the asked questions is for linux and all of them seems to have solved the problem, but when I try to replicate on the macos it just don't work.大部分被问到的问题是针对 linux 的,它们似乎都解决了问题,但是当我尝试在 macos 上进行复制时,它就不起作用了。

Maybe there is some step that I'm missing, or people already know that they have to do in some of the steps, but i'm failing miserably.也许我错过了一些步骤,或者人们已经知道他们必须在某些步骤中做,但我失败得很惨。

Some of the steps that I tried:我尝试的一些步骤:

create use user and group jenkins:创建使用用户和组 jenkins:

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

created the group docker:创建组 docker:

sudo dscl . create /Groups/docker gid 1001

Added the jenkins user to the docker group将 jenkins 用户添加到 docker 组

sudo dscl . append /Groups/docker GroupMembership jenkins

Checked if the user really is on the group检查用户是否真的在组中

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

Tried to change the owner of the socket from inside the jenkins container (that's why I was building the image, but it didn't work)试图从 jenkins 容器内部更改套接字的所有者(这就是我构建映像的原因,但它不起作用)

Tried to changer the ownership of the socket on the host macos but it just don't change.试图更改主机 macos 上套接字的所有权,但它只是没有改变。 The socket is always with those permissions.套接字始终具有这些权限。

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

For jenkins, the best is to have agents that will run all jobs and the master that will only do the orchestration jobs.对于 jenkins,最好的方法是拥有将运行所有作业的代理和只执行编排作业的主服务器。

Some years ago, I build an JNLP agent that register itself to jenkins master, you can check my repo here: https://github.com/jmaitrehenry/docker-jenkins-jnlp As I say, I made it like 3 years ago and may be a bit outdated.几年前,我构建了一个注册到 jenkins master 的 JNLP 代理,你可以在这里查看我的仓库: https://github.com/jmaitrehenry/docker-jenkins-jnlp正如我所说,我做到了就像 3 年前一样可能有点过时了。

About your problem, you need to know that Docker for Mac run containers inside a little VM, so when you add a user on MacOS, the VM doesn't have it.关于您的问题,您需要知道 Docker for Mac 在一个小 VM 内运行容器,因此当您在 MacOS 上添加用户时,VM 没有它。 And Docker for Mac do a lot a magic to map uid inside your mac with some uid inside containers.和 Docker for Mac 对 Mac 内的 map uid 和容器内的一些 uid 做了很多魔术。

You can try to add the docker client inside your Dockerfile, for that, try to add those steps:您可以尝试在 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

You need to enable host mode networking by adding network: host to your compose file:您需要通过将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

This will allow your guest docker container to see the hosts network.这将允许您的访客 docker 容器查看主机网络。 The problem is that Docker Desktop for MacOS doesn't support listening over the TCP port.问题是 Docker Desktop for MacOS 不支持通过 TCP 端口进行监听。 There is a known workaround by using socat.使用 socat 有一个已知的解决方法。 https://www.ivankrizsan.se/2016/05/21/docker-api-over-http-on-mac-os-x-with-docker-for-mac-beta/ . https://www.ivankrizsan.se/2016/05/21/docker-api-over-http-on-mac-os-x-with-docker-for-mac-beta/ Once you have socat set up to route from the docker.socker to TCP 2376 set your Host URI to tcp://0.0.0.0:2376.将 socat 设置为从 docker.socker 路由到 TCP 2376 后,将主机 URI 设置为 ZE20BB202B1D5537B1415E3263A37ED738Z:://0.0.0。 And of course you will need to create a new Dockerfile to extend the jenkins/jenkins:lts one with FROM jenkins/jenkins:lts and add Docker to the container as suggested in another answer当然,您需要创建一个新的 Dockerfile 以使用FROM jenkins/jenkins:lts lts 扩展 jenkins/jenkins:lts 并按照另一个答案中的建议将 Docker 添加到容器中

I ran into the same issue.我遇到了同样的问题。 jenkins user was not able to run docker commands even after adding the user to docker group.即使将用户添加到 docker 组后, jenkins用户也无法运行 docker 命令。

When I checked the permissions on the host machine (MacOS), docker.sock file was owned by root:daemon .当我检查主机(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/....

I updated the permissions to 755 and it started working.我将权限更新为755并开始工作。 I am able to run the docker commands on the container as jenkins user.我能够以jenkins用户身份在容器上运行 docker 命令。

Please change the host file permissions only for development environment.请仅更改开发环境的主机文件权限。

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

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