代码之家  ›  专栏  ›  技术社区  ›  tehbeardedone

对Jenkins slave发出docker命令

  •  1
  • tehbeardedone  · 技术社区  · 6 年前

    我有一个Jenkins master在Windows Server 2016上运行。我需要能够运行linux容器来运行一些自动化的e2e测试。由于我无法进入的原因,我无法在此计算机上启用hyper-v。这使我无法在我的詹金斯主机上安装lcow和docker

    我所做的是在virtualbox中安装一个Ubuntu 18.04vm并在那里安装docker。我已经将VM配置为Jenkins从机,使用ssh作为 jenkins 用户。我已经为这个用户设置和配置了一切,使其能够在不使用 sudo . 如果我以jenkins用户的身份手动ssh到服务器,我可以运行docker命令而不会出现问题。一切都按你所期望的方式进行。

    然后,我设置了一个测试构建,以检查一切是否正常工作。问题是,当我尝试使用 Execute Shell 建立步骤我得到一个 docker: not found 错误。据我所知,构建是以正确的用户身份运行的。我补充说 who -u 转到生成步骤,以便我可以检查生成作为哪个用户运行。

    以下是我的版本的输出:

    [TEST - e2e - TEST] $ /bin/sh -xe /tmp/jenkins16952572249375249520.sh
    + who -u
    jenkins  pts/0        2018-08-10 16:43   .         10072 (10.0.2.2)
    + docker run hello-world
    /tmp/jenkins16952572249375249520.sh: 3: /tmp/jenkins16952572249375249520.sh: docker: not found
    

    如前所述,jenkins用户已经添加到docker组,docker已经添加到 $PATH ( /snap/bin/

    jenkins@jenkins-docker-slave:~$ which docker
    /snap/bin/docker
    jenkins@jenkins-docker-slave:~$ $PATH
    -bash:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin: No such file or directory
    jenkins@jenkins-docker-slave:~$ who -u
    jenkins  pts/0        2018-08-10 16:43   .         10072 (10.0.2.2)
    jenkins@jenkins-docker-slave:~$ cat /etc/group | grep docker
    docker:x:1001:qctesting,jenkins
    

    从这段代码中可以看到,我可以作为jenkins用户登录到服务器,从而成功运行docker命令:

    jenkins@jenkins-docker-slave:~$ docker run hello-world
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
     3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
    
    To try something more ambitious, you can run an Ubuntu container with:
     $ docker run -it ubuntu bash
    
    Share images, automate workflows, and more with a free Docker ID:
     https://hub.docker.com/
    
    For more examples and ideas, visit:
     https://docs.docker.com/engine/userguide/
    

    我还配置了从节点属性中的docker路径,因为我认为它可以解决我的问题。如你所见,我列出了git和docker。Git命令运行得很好。只有docker命令给了我麻烦。我都试过了 /snap/bin /snap/bin/docker 没有运气。

    enter image description here

    我正在尝试构建一个jenkins作业,它将克隆一个git repo,将需要使用的容器旋转起来 docker-compose 以及一些在构建时传入的构建参数,并针对任何环境(qa、staging、production等)运行我的e2e测试。我就是不能让詹金斯的奴隶来执行码头工人的命令。我错过了什么?如何让从机识别系统上已经安装了docker,并且用户有正确的权限执行这些命令。

    我不是想在docker里运行docker。实际上,我在jenkins从机上运行docker命令时发现的所有问题/文档都描述了如何通过在docker容器中运行从机并在从机容器中安装docker客户端来解决此问题。这不是我想要完成的。我正试图从一个jenkins主服务器ssh到一个jenkins从服务器,它已经安装了docker,并在服务器上运行docker命令作为 用户。

    1 回复  |  直到 6 年前
        1
  •  1
  •   tehbeardedone    6 年前

    我终于明白了,多亏了 this question . 在阅读了答案之后,我意识到我在Ubuntu上安装了错误的docker版本。我删除了以前的安装,并使用 sudo curl -sSL https://get.docker.com/ | sh