• Uncategorized

About docker : Docker-not-found-when-building-docker-image-using-Docker-Jenkins-container-pipeline

Question Detail

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/[email protected]/durable-
ebc35179/script.sh: 2: /var/jenkins_home/workspace/simple-tdd-
[email protected]/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

Question Answer

You’re missing the docker client. Install it as this in Dockerfile:

RUN curl -fsSLO https://get.docker.com/builds/Linux/x86_64/docker-17.04.0-ce.tgz \
  && tar xzvf docker-17.04.0-ce.tgz \
  && mv docker/docker /usr/local/bin \
  && rm -r docker docker-17.04.0-ce.tgz

Source

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.

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}"
    }

Edit: May 2018

As pointed by Guillaume Husta, this jpetazzo’s blog article discourages this technique:

Former versions of this post advised to bind-mount the docker binary from the host to the container. This is not reliable anymore, because the Docker Engine is no longer distributed as (almost) static libraries.

Docker client should be installed inside a container as described here. Also, jenkins user should be in docker group, so execute following:

$ docker exec -it -u root my-jenkins /bin/bash
# usermod -aG docker jenkins

and finally restart my-jenkins container.

Original answer:

You could use host’s docker engine like in this @Adrian Mouat blog article.

 docker run -d \
   --name my-jenkins \
   -v /var/jenkins_home:~/.jenkins \
   -v /var/run/docker.sock:/var/run/docker.sock \
   -p 8080:8080 jenkins

This avoids having multiple docker engine version on host and jenkins container.

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

This section helped me install docker inside the jenkins container: https://www.jenkins.io/doc/book/installing/docker/#downloading-and-running-jenkins-in-docker

Also, I had to replace FROM jenkins/jenkins:2.303.1-lts-jdk11 in the Dockerfile in step 4(a) with jenkins/jenkins.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.