在Jenkins从属节点上运行的Jenkinsfile中执行docker build命令的最简单方法? [英] Easiest way to do docker build command within Jenkinsfile running on Jenkins slave node?

查看:408
本文介绍了在Jenkins从属节点上运行的Jenkinsfile中执行docker build命令的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要我的Jenkinsfile做的基本示例:

Basic example of what I want my Jenkinsfile to do:

node {
   sh 'docker build -t foo/bar .'
}

似乎我需要将docker安装到执行我的Jenkinsfile的Jenkins从属映像上.有一个简单的方法可以做到这一点吗? (那个Jenkins从属映像本身就是一个docker容器)

It seems like I need to install docker onto the Jenkins slave image that's executing my Jenkinsfile. Is there an easy way of doing this? (That Jenkins slave image is itself a docker container)

我的假设正确吗?

  1. 与Jenkins主/从服务器一起运行时,Jenkins文件由Jenkins从属执行
  2. 通过管理插件"部分安装的Jenkins插件(例如Docker插件或Gcloud SDK插件)仅安装在Jenkins主服务器上,因此我需要手动构建我的Jenkins从docker镜像并在该镜像上安装docker? /li>
  1. When running with Jenkins master/slaves, the Jenkinsfile is executed by a Jenkins slave
  2. Jenkins plugins installed via the Manage Plugins section (e.g. the Docker Plugin, or Gcloud SDK plugin) are only installed on the Jenkins masters, therefore I would need to manually build my Jenkins slave docker image and install docker on the image?

由于我还需要访问'gcloud'命令(我正在通过Kubernetes Helm/Charts运行Jenkins),所以我一直在为我的Jenkins从属服务器使用gcr.io/cloud-solutions-images/jenkins-k8s-slave映像.

Since I also need access to the 'gcloud' command (I'm running Jenkins via Kubernetes Helm/Charts), I've been using the gcr.io/cloud-solutions-images/jenkins-k8s-slave image for my Jenkins slave.

目前,它会错误地提示"docker:未找到"

Currently it errors out saying "docker: not found"

推荐答案

我的假设是您想在Jenkins奴隶(这是一个Kubernetes吊舱)内docker build,我假设由

My assumption is that you want to docker build inside the Jenkins slave (which is a Kubernetes pod, I assume created by the Kubernetes Jenkins Plugin)

要设置阶段,当Kubernetes创建将充当Jenkins从属的pod时,您在节点内执行的所有命令都将在该Kubernetes pod内的一个容器中执行(默认情况下,只有一个容器)容器,但稍后会详细介绍.)

To set the stage, when Kubernetes creates pod that will act as a Jenkins slave, all commands that you execute inside the node will be executed inside that Kubernetes pod, inside one of the containers there (by default there will only be one container, but more on this later).

因此,您实际上正在尝试在基于gcr.io/cloud-solutions-images/jenkins-k8s-slave的容器内运行Docker命令,该容器很可能基于 Jenkins官方JNLP Slave 不包含Docker!

So you are actually trying to run a Docker command inside a container based on gcr.io/cloud-solutions-images/jenkins-k8s-slave, which is most likely based on the official Jenkins JNLP Slave, which does not container Docker!

从现在开始,您可以采用两种方法:

From this point forward, there are two approaches that you can take:

  • use a slightly modified image based on the JNLP slave that also contains the Docker client and mount the Docker socket (/var/run/docker.sock) inside the container. (You can find details on this approach here). Here is an image that contains the Docker client and kubectl.

这是有关如何配置Jenkins插件的完整视图:

Here is a complete view of how to configure the Jenkins Plugin:

请注意,您使用不同的映像(可以创建自己的映像并在其中添加任何所需的二进制文件),并将Docker套接字安装在容器内.

Note that you use a different image (you can create your own and add any binary you want there) and that you mount the Docker socket inside the container.

  • 第一种方法的问题是您创建了一个从官方JNLP从站派生的新映像,并手动添加了Docker客户端.这意味着每当Jenkins或Docker进行更新时,您都需要手动更新映像和整个配置,这不是您所希望的. 使用第二种方法,您始终使用官方映像,并且使用JNLP从属服务器在同一容器中启动其他容器.
    • the problem with the first approach is that you create a new image forked from the official JNLP slave and manually add the Docker client. This means that whenever Jenkins or Docker have updates, you need to manually update your image and entire configuration, which is not that desirable. Using the second approach you always use official images, and you use the JNLP slave to start other containers in the same pod.
    • 这是下图中的完整文件

      这是Jenkins插件文档,

      正如我所说,JNLP映像将启动您在同一容器中指定的容器.请注意,为了从容器中使用Docker,您仍然需要挂载Docker袜子.

      As I said, the JNLP image will start a container that you specify in the same pod. Note that in order to use Docker from a container you still need to mount the Docker sock.

      这是我发现的在容器内运行的Jenkins JNLP从站内实现构建映像的两种方法.

      These are the two ways I found to achieve building images inside a Jenkins JNLP slave running inside a container.

      该示例还显示了如何使用Jenkins的凭据绑定推送映像,以及如何在构建过程中更新Kubernetes部署.

      The example also shows how to push the image using credential bindings from Jenkins, and how to update a Kubernetes deployment as part of the build process.

      更多资源:

      谢谢, 拉杜M

      这篇关于在Jenkins从属节点上运行的Jenkinsfile中执行docker build命令的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆