简体   繁体   English

如何使用Jenkins管道构建Docker映像?

[英]How to build docker image using Jenkins pipeline?

By using jenkins, I create an item of "Pipeline" type. 通过使用詹金斯,我创建了一个“管道”类型的项目。 And I set "Pipeline from SCM" to get Jenkinsfile . 然后将“ SCM中的管道”设置为Jenkinsfile You can check my GitHub repository : 您可以检查我的GitHub存储库

I want use Jenkins pipeline to build a docker image. 我想使用Jenkins管道构建docker映像。 Here is the Jenkinsfile: 这是Jenkinsfile:

node {
   sh "docker build -t 192.168.59.224:5000/ubuntu-test ."
}

The Dockerfile is also very simple: Dockerfile也非常简单:

FROM ubuntu:14.04

RUN sudo apt-get update && sudo apt-get install -y wget

When I run the project. 当我运行项目时。 I got following error: 我收到以下错误:

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /var/jenkins_home/workspace/test/Dockerfile: no such file or directory

Here is the full console output 这是完整的控制台输出

Started by user kai
[Pipeline] node
Running on master in /var/jenkins_home/workspace/test
[Pipeline] {
[Pipeline] sh
[test] Running shell script
+ docker build -t 192.168.59.224:5000/ubuntu-test .
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /var/jenkins_home/workspace/test/Dockerfile: no such file or directory
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

I checked the workspace: 我检查了工作区:

ls /var/jenkins_home/workspace/test/


ls /var/jenkins_home/workspace/test@script/
Dockerfile
Jenkinsfile

There are nothing in test directory, but both Jenkinsfile and Dockerfile are in test@script directory. 测试目录中什么都没有,但是Jenkinsfile和Dockerfile都在test @ script目录中。

It seems that Jenkins only get the Jenkins from the repository. 似乎Jenkins仅从存储库中获取Jenkins。 When it execute the Jenkinsfile, it can not build the docker image without Dockerfile. 当执行Jenkinsfile时,没有Dockerfile便无法构建Docker映像。

How can I solve the problem? 我该如何解决这个问题?

You're not instructing Jenkins to check out your repository. 您不是在指示Jenkins检出您的存储库。 You do this by adding checkout scm before calling docker. 您可以通过在调用checkout scm之前添加checkout scm Like this: 像这样:

node {
  checkout scm
  sh "docker build -t 192.168.59.224:5000/ubuntu-test ."
}

The variable scm is set by Jenkins when you use "Pipeline from SCM" and points to the location where Jenkins got the Jenkinsfile from. 当您使用“来自SCM的管道”时,变量scm由Jenkins设置,并指向Jenkins从中获取Jenkinsfile的位置。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM