简体   繁体   English

如何在 Jenkins 从节点上运行 Dockerfile 代理?

[英]How to run Dockerfile agent on a Jenkins Slave Node?

Please help me with below.请在下面帮助我。 I have Jenkins installed on one shared Server.我在一台共享服务器上安装了 Jenkins。 currently used by multiple teams so no additional packages/software are allowed to install there.目前被多个团队使用,因此不允许在那里安装额外的软件包/软件。 Each team has their dedicated slave nodes added where they configure as per their need.每个团队都有自己的专用从节点添加到他们根据需要进行配置的位置。 In Same way, we have one dedicated node to run our jobs.(this is where I want to run my job too)同样,我们有一个专用节点来运行我们的工作。(这也是我想运行我的工作的地方)

I have bitbucket repo, with " Jenkinsfile " which will have deployment steps and " Dockerfile " where we like to created container from it and run the deployment steps in it.我有 bitbucket 存储库,其中“ Jenkinsfile ”将具有部署步骤和“ Dockerfile ”,我们希望从中创建容器并在其中运行部署步骤。

I'm trying to test this first with some example.我试图先用一些例子来测试这个。 so I have " Dockerfile " like this.所以我有这样的“ Dockerfile ”。

#This image is Developed from ubuntu:18.04-Dockerhub
FROM ubuntu:18.04

#Updating System Packages and installing required packages
RUN apt-get update && \
    apt-get install -y openssh-server wget git curl zip unzip && \
    apt-get clean

#Installing rsync
RUN apt-get install -y rsync 

where I have " Jenkinsfile " like below.我在哪里有“ Jenkinsfile ”,如下所示。

pipeline {
    agent { node { label 'slave_node' } }
    stages {
        stage('Test') {
            agent {
                dockerfile true
            }
            steps {
                sh 'cat /etc/os-release'
                sh 'curl --version'
                sh 'echo Successfully compiled'
            }
        }
    }
} 

when I execute this Pipeline job,当我执行这个管道作业时,

  • Its gets the Jenkins file.它获取 Jenkins 文件。
  • starts correctly on Slave node mentioned.在提到的从节点上正确启动。
  • checkouts the repo code.签出回购代码。
  • proceeds to stages分阶段进行
  • but when it comes to stage('Test'), Node changes back to "Master Jenkins" and again its starts checking out repo code.但是当涉及到阶段('Test')时,Node 变回“Master Jenkins”并再次开始检查 repo 代码。 [this is where I have issue. [这是我有问题的地方。 I don't know why this is getting switched]我不知道为什么会切换]
  • proceeds to stages分阶段进行
  • Run the "Docker File"运行“Docker 文件”
  • Try to pull the Image from Hub.尝试从集线器中提取图像。 but fails the job as "Docker command not found" (expected error as Master node doesn't have the setup.但由于“找不到 Docker 命令”而使作业失败(预期错误,因为主节点没有设置。

Question is: Why Job getting switched from Slave_node to master_node?问题是:为什么 Job 从 Slave_node 切换到 master_node? Please help.请帮忙。 thanks in advance.提前致谢。

If I run this on my Personal laptop, Its works perfectly fine.如果我在我的个人笔记本电脑上运行它,它工作得非常好。

I would check out your Docker settings in Jenkins.我会在 Jenkins 中检查您的 Docker 设置。 It may be that you've defined the master node as the default Jenkins agent, so when you run with only 'dockerfile: true' it attempts to run the build on the master node.可能您已将主节点定义为默认 Jenkins 代理,因此当您仅使用 'dockerfile: true' 运行时,它会尝试在主节点上运行构建。

You can find the reference to this specific option by searching for 'Specifying a Docker Label' in this documentation.您可以通过在本文档中搜索“指定 Docker 标签”来找到对此特定选项的参考。

https://www.jenkins.io/doc/book/pipeline/docker/ https://www.jenkins.io/doc/book/pipeline/docker/

Referring to https://www.jenkins.io/doc/book/pipeline/docker/ I was able to achieve my need using, scripted pipeline.参考https://www.jenkins.io/doc/book/pipeline/docker/我能够使用脚本化管道来满足我的需求。 so posting that as answer here.所以在这里发布作为答案。

but I still don't know whats wrong with Declarative pipeline.但我仍然不知道声明式管道有什么问题。

node ('slave_node') {
    checkout scm
    def customImage = docker.build("custom-image-name")
    customImage.inside {
        sh 'Inside Container'
        sh 'cat /etc/os-release'
    }
}

Thanks谢谢

Found another way to achieve this.找到了另一种方法来实现这一点。 we can use dockerfile funtion itself to combine with any agent node.我们可以使用 dockerfile 功能本身与任何代理节点结合。

example as below示例如下

#!/usr/bin/env groovy
pipeline {
  agent {
    dockerfile {
      filename 'Dockerfile'
      label 'slave_node'
    }
  }
  stages {
    stage("example stage") {
      steps {
        script {
          sh 'cat /etc/os-release'
          sh 'curl --version'
          sh 'echo Successfully compiled'
        }
      }
    }
  }
}

source can be found here: syntax and search for keyword "dockerfile" to find the details.可以在此处找到源:语法并搜索关键字“dockerfile”以查找详细信息。

The following code has also worked, this can be used if you don't want to build the docker image in real-time.以下代码也有效,如果您不想实时构建 docker 图像,可以使用此代码。 So there is no need for Dockerfile, we can directly specify the image name.所以不需要Dockerfile,我们可以直接指定镜像名称。

pipeline {
    agent {
        docker { 
            image 'image-name'                 
            label 'common-build-machine' 
        }
    }
...
}

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

相关问题 如何在詹金斯的从节点中选择 docker 代理 - how docker agent is selected in slave node in jenkins Jenkins:dockerfile代理命令未在容器中运行 - Jenkins: dockerfile agent commands not run in container 如何构建可以通过 Jenkins master 运行节点基础项目的基于节点的 dockerized jenkins-slave - how to build node based dockerized jenkins-slave that can run the node base project through Jenkins master 如何在特定的奴隶中限制jenkins管道码头代理? - How to restrict the jenkins pipeline docker agent in specific slave? 如何在基于 dockerfile 的容器中运行 Jenkins 管道? - How to run Jenkins pipeline in a container based on a dockerfile? Dockerfile创建映像以用作jenkins中的容器从属 - Dockerfile to create an image to be used as an container slave in jenkins 为什么在使用 Jenkins dockerfile 代理时它会使用无效用户运行容器? - Why when using Jenkins dockerfile agent does it run container with invalid user? 用Docker运行Jenkins master和slave - Run Jenkins master and slave with Docker 如何使用 Jenkins docker-swarm-plugin 使用 docker 图像 jenkins/jnlp-slave(传递代理名称) - How can I user docker image jenkins/jnlp-slave with Jenkins docker-swarm-plugin (pass agent name) 如何在一个mesos从节点上运行多个docker容器? - How to run multiple docker container on one mesos slave node?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM