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

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

问题描述

我希望 Jenkinsfile 执行的基本示例:

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

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

我的假设正确吗?

  1. 当与 Jenkins master/slave 一起运行时,Jenkinsfile 由 Jenkins slave 执行
  2. 通过管理插件部分安装的 Jenkins 插件(例如 Docker 插件或 Gcloud SDK 插件)仅安装在 Jenkins 主机上,因此我需要手动构建我的 Jenkins 从属 docker 镜像并在镜像上安装 docker?

由于我还需要访问gcloud"命令(我通过 Kubernetes Helm/Charts 运行 Jenkins),我一直在使用 gcr.io/cloud-solutions-images/jenkins-k8s-我的 Jenkins 奴隶的奴隶图像.

目前错误提示docker: not found"

解决方案

我的假设是你想 docker build 在 Jenkins slave(这是一个 Kubernetes pod,我假设由

<块引用>

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

  • 第一种方法的问题在于,您创建了一个从官方 JNLP 从站派生的新镜像并手动添加 Docker 客户端.这意味着每当 Jenkins 或 Docker 有更新时,您都需要手动更新您的镜像和整个配置,这是不可取的.使用第二种方法,您始终使用官方镜像,并且您使用 JNLP 从属设备在同一个 pod 中启动其他容器.
<块引用>

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

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

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

更多资源:

谢谢,拉杜M

Basic example of what I want my Jenkinsfile to do:

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

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)

Are my assumptions correct?

  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?

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.

Currently it errors out saying "docker: not found"

解决方案

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)

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).

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:

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

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.

  • 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.

Here is the full file from the image below

Here is the Jenkins Plugin documentation for doing this

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.

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

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.

Some more resources:

Thanks, Radu M

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

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