Docker not found when building docker image using Docker Jenkins container pipeline

Viewed 67072

I have a Jenkins running as a docker container, now I want to build a Docker image using pipeline, but Jenkins container always tells Docker not found.

[simple-tdd-pipeline] Running shell script
+ docker build -t simple-tdd .
/var/jenkins_home/workspace/simple-tdd-pipeline@tmp/durable-
ebc35179/script.sh: 2: /var/jenkins_home/workspace/simple-tdd-
pipeline@tmp/durable-ebc35179/script.sh: docker: not found

Here is how I run my Jenkins image:

docker run --name myjenkins -p 8080:8080 -p 50000:50000 -v 
/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock 
jenkins

And the DockerFile of Jenkins image is: https://github.com/jenkinsci/docker/blob/9f29488b77c2005bbbc5c936d47e697689f8ef6e/Dockerfile

7 Answers

In your Jenkins interface go to "Manage Jenkins/Global Tool Configuration"

Then scroll down to Docker Installations and click "Add Docker". Give it a name like "myDocker"

Make sure to check the box which says "Install automatically". Click "Add Installer" and select "Download from docker.com". Leave "latest" in the Docker version. Make sure you click Save.

enter image description here

In your Jenkinsfile add the following stage before you run any docker commands:

 stage('Initialize'){
        def dockerHome = tool 'myDocker'
        env.PATH = "${dockerHome}/bin:${env.PATH}"
    }

The problem is in your Jenkins, it isn't capable to use the docker engine, even if you do install the docker from the plugin manager. From what I got researching there are some alternatives to workaround this issue:

1: Build a image using some docker image with pre-installed docker in it like provided by getintodevops/jenkins-withdocker:lts

2: Build the images from jenkins/jenkins mounting the volumes to your host then install the docker all by yourself by creating another container with same volumes and executing the bash cmd to install the docker or using Robert suggestion

docker run -p 8080:8080 -p 50000:50000 -v $HOME/.jenkins/:/var/jenkins_home 
-v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:latest

or 3: The most simple, just add the installed docker path from your host machine to be used by your jenkins container with: -v $(which docker):/usr/bin/docker

Your docker command should look like this:

docker run \
--name jenkins --rm \
-u root -p 8080:8080 -p 50000:50000 \
-v $(which docker):/usr/bin/docker\
-v $HOME/.jenkins/:/var/jenkins_home 
-v /var/run/docker.sock:/var/run/docker.sock \
jenkins/jenkins:latest

[Source]https://forums.docker.com/t/docker-not-found-in-jenkins-pipeline/31683

Extra option: Makes no sense if you just want to make use of a single Jenkis server but it's always possible to install a OS like Ubuntu using an image and install the jenkins .war file from there

docker run -d \
--group-add docker \
-v $(pwd)/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker \
-p 8080:8080 -p 50000:50000 \
jenkins/jenkins:lts

Just add option --group-add docker when docker run.

Add docker path i.e -v $(which docker):/usr/bin/docker to container in volumes like

 docker run -d \
   --name my-jenkins \
   -v $(which docker):/usr/bin/docker \
   -v /var/jenkins_home:~/.jenkins \
   -v /var/run/docker.sock:/var/run/docker.sock \
   -p 8080:8080 jenkins
Related