简体   繁体   中英

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 . 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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