Issue
I'm trying to run Dockerfile in Jenkinsfile pipeline. I have the following Dockerfile:
FROM ubuntu:latest
ENV VERSION=1.2.0
RUN apt-get update -y
RUN apt-get install -y python vim zip unzip
RUN mkdir -p /tmp
WORKDIR /tmp
COPY zip_job.py ./
RUN cat /etc/lsb-release
RUN uname -m
RUN ls -l /tmp
This is my Jenkinsfile:
pipeline {
agent { dockerfile true }
stages {
stage('Build') {
steps {
sh 'python zip_job.py'
}
}
stage('Publish') {
steps {
rtServer (
id: 'Artifactory-1',
url: 'https://aidock.jfrog.io/artifactory/devops-assignment/',
username: 'super-user'
password: 'Qw12856!'
)
rtUpload (
serverId: 'Artifactory-1',
spec: '''{
"files": [
{
"pattern": "tmp/*.zip",
"target": "binary-storage/1.2.0
}
]
}''',
)
}
}
stage('Report') {
steps {
echo 'Hello World'
}
}
stage('Cleanup') {
steps {
echo 'Hello World'
}
}
}
}
My working directory in Docker should be /tmp. When running a build I get the following error:
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/My-Pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Agent Setup)
[Pipeline] isUnix
[Pipeline] readFile
[Pipeline] sh
+ docker build -t b96873ea12524b982ef8ce77660ade7f3744e812 -f Dockerfile .
/var/jenkins_home/workspace/My-Pipeline@tmp/durable-f53af459/script.sh: 1: /var/jenkins_home/workspace/My-Pipeline@tmp/durable-f53af459/script.sh: docker: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE
Tried almost every trick in the book but yet it is not working. Docker Plugins are installed.
Appreciate the help. Thanks!
Solution
Execute the Pipeline, or stage, with a container built from a Dockerfile contained in the source repository. In order to use this option, the Jenkinsfile must be loaded from either a Multibranch Pipeline or a Pipeline from SCM. Conventionally this is the Dockerfile in the root of the source repository: agent { dockerfile true }. If building a Dockerfile in another directory, use the dir option: agent { dockerfile { dir 'someSubDir' } }. If your Dockerfile has another name, you can specify the file name with the filename option. You can pass additional arguments to the docker build …command with the additionalBuildArgs option, like agent { dockerfile { additionalBuildArgs '--build-arg foo=bar' } }. For example, a repository with the file build/Dockerfile.build, expecting a build argument version:
agent {
// Equivalent to "docker build -f Dockerfile.build --build-arg version=1.0.2 ./build/
dockerfile {
filename 'Dockerfile.build'
dir 'build'
label 'my-defined-label'
additionalBuildArgs '--build-arg version=1.0.2'
args '-v /tmp:/tmp'
}
}
Answered By - hagay
Answer Checked By - Cary Denson (JavaFixing Admin)