如何创建构建Docker映像的Jenkins管道

时间:2019-07-04 20:45:42

标签: docker ubuntu jenkins jenkins-pipeline

我是Docker和Jenkins的新手,我正在尝试创建一个构建Docker映像的Jenkins Pipeline。 在尝试构建并继续收到此错误时,我陷入了困境:

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

  1. 我已经在VM上安装了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

1 个答案:

答案 0 :(得分:0)

TL; DR :您的Jenkins Agent中必须装有Docker。

按照您上面描述的过程,我使用最新的jenkins/jenkins image from Docker Hub启动并运行了Jenkins。在查看了容器的文件系统后,我确认了我在对您的问题的评论中所猜测的内容:不在Jenkins容器中 安装了Docker。假设您将Jenkins Master服务器用作管道作业的代理,那么您会想到以下两种选择:

  1. 扩展现有的docker容器-在新的docker文件中使用a=open("cards.json") b=a.read() data=[] data.append(b) count=0 for elem in data: try: content=json.loads(elem) except: print content exit() for j in content: count=count+1 if j['layout']=='normal' and j['digital']==False: url=str(j['image_uris']['normal']) final=url[url.find('normal')+6:] print (url) print("a") i1=urllib.urlretrieve(url) print("b") i2=i1.read() file=open(str(count),'wb') file.write(i2) file.close() if count>5: exit() 之类的东西-包括您的依赖项。
  2. 将现有的docker守护程序从主机绑定到Jenkins容器的运行时。

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

FROM jenkins/jenkins

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