【问题标题】:How to create a Jenkins pipeline that builds a Docker image如何创建构建 Docker 映像的 Jenkins 管道
【发布时间】:2019-11-15 13:22:04
【问题描述】:

我是 Docker 和 Jenkins 的新手,我正在尝试创建一个构建 Docker 映像的 Jenkins 流水线。 我在尝试构建并不断收到此错误时被卡住了:

/var/jenkins_home/workspace/Docker-Pipeline@tmp/durable-a11b32f8/script.sh:第 1 行:docker:找不到命令

  1. 我已经在虚拟机上安装了 ubuntu。
  2. 已安装 docker。
  3. 从 dockerhub 安装 jenkins/jenkins。
  4. 其余部分我按照本教程进行: https://www.youtube.com/watch?v=z32yzy4TrKM&t=147s

我正在做与他完全相同的事情,但总是失败。

Started by user admin
Obtained Jenkinsfile from git https://github.com/naorca/NodeApp.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start o

f Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/Docker-Pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Clone repository)
[Pipeline] checkout
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/naorca/NodeApp.git # timeout=10
Fetching upstream changes from https://github.com/naorca/NodeApp.git
 > git --version # timeout=10
 > git fetch --tags --progress https://github.com/naorca/NodeApp.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision b74538f2f34b6c28306fcca8119e215b87124e5e (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f b74538f2f34b6c28306fcca8119e215b87124e5e
Commit message: "Update Jenkinsfile"
 > git rev-list --no-walk b74538f2f34b6c28306fcca8119e215b87124e5e # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build image)
[Pipeline] sh
+ docker build -t naorca/nodeapp .
/var/jenkins_home/workspace/Docker-Pipeline@tmp/durable-a11b32f8/script.sh: line 1: docker: command not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

【问题讨论】:

  • 听起来 Docker 没有安装在您的 Jenkins 代理(FKA,“Jenkins Slave”)中。根据您描述的设置,我会冒险猜测您正在使用主服务器作为构建代理。这应该没问题,但这需要将 Docker 安装在正在运行的 jenkins/jenkins 容器内。如果 Docker 实际上已安装在容器中,那么PATH 环境变量中可能缺少 Docker 可执行文件包含目录的路径,这将阻止您的管道调用它。

标签: docker ubuntu jenkins jenkins-pipeline


【解决方案1】:

TL;DR:您的 Jenkins 代理中必须有 Docker。

按照您上面描述的过程,我使用最新的jenkins/jenkins image from Docker Hub 启动并运行了 Jenkins。查看容器的文件系统后,我确认了我在对您的问题的评论中推测的内容:Docker 未安装 in Jenkins 容器。假设您使用 Jenkins 主服务器作为流水线作业的代理,您会想到几个选项:

  1. 扩展现有 docker 容器 - 在新 docker 文件中使用 FROM jenkins/jenkins 之类的内容 - 以包含您的依赖项。
  2. 将您现有的 docker 守护程序从主机绑定到 Jenkins 容器的运行时。

虽然我偏爱第一个解决方案,但我在 Docker 论坛上找到了第二个解决方案的实现:“Using docker in a dockerized Jenkins container”然后我尝试了该解决方案,并且可以确认 Docker 在 Jenkins Master 中为我提供使用以下命令启动 Jenkins 容器后的容器:

docker run -v /var/run/docker.sock:/var/run/docker.sock \
    -v $(which docker):$(which docker) \
    -p 8000:8080 \
    -p 50000:50000 \
    jenkins/jenkins

我不确定,但我想由于将其自己的 Docker 套接字和 Docker 可执行文件安装到容器中,可能会对 Jenkins Master 的主机产生一些负面的安全影响;但是,我会把这留给更了解 Docker 内部的人来确定。无论如何,我可以确认上述解决方案确实有效。

【讨论】:

  • 谢谢你!您能否指导我或指导我阅读描述如何配置 Jenkins 代理(FKA,“Jenkins Slave”)的指南?它是否需要在不同的虚拟机上?真的很感激!
  • Jenkins 代理可以通过节点部分添加。我相信它的 URI 是 /computer/,但您也可以通过单击左侧菜单中的“构建执行程序状态”来到达那里。然后您可以从选项列表中选择“新节点”。 Jenkins Master 可以充当自己的代理,这对于单用户/单团队 Jenkins 服务器来说很好;但是,如果您打算让您的 Jenkins 服务器可供多个用户/团队使用,那么您应该创建独立的代理。我建议阅读 Jenkins Wiki 上的 Distributed Builds
猜你喜欢
  • 2018-03-27
  • 2017-07-16
  • 2018-06-15
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多