2

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 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, make sure that /var/jenkins_home/workspace/test/Dockerfileis checked out correctly. I would also suggest that you edit your question and add full log.
I've checked the workspace and add full log. Please check, THX.
Thanks! It's pretty clear that Jenkins doesn't check out the source in the workspace (not the test@script, that is just used by Jenkins for retrieving the Jenkinsfile). So is the log from my suggested pipeline script above (with checkout scm) or is it your initial? If you have an scm checkout you should see something like Checking out Revision ... in the logs.
Thank you! I made a mistake: I did not update the git repository correctly!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.