How to build docker image using Jenkins pipeline?

℡╲_俬逩灬. 提交于 2019-12-22 18:22:44

问题


By using jenkins, I create an item of "Pipeline" type. And I set "Pipeline from SCM" to get Jenkinsfile. You can check my GitHub repository:

I want use Jenkins pipeline to build a docker image. Here is the Jenkinsfile:

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

The Dockerfile is also very simple:

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.

It seems that Jenkins only get the Jenkins from the repository. When it execute the Jenkinsfile, it can not build the docker image without Dockerfile.

How can I solve the problem?


回答1:


You're not instructing Jenkins to check out your repository. You do this by adding checkout scm before calling docker. 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.



来源:https://stackoverflow.com/questions/42453967/how-to-build-docker-image-using-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!