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

56,473

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
56,473
kitko112
Author by

kitko112

Updated on September 02, 2021

Comments

  • kitko112
    kitko112 over 2 years

    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

  • kitko112
    kitko112 almost 7 years
    I added the above script and built the dockerfile, it failed with below error: curl: (23) Failed writing body (0 != 16384)
  • kitko112
    kitko112 almost 7 years
    I can run the command now, just changing user as root before the curl command. USER root
  • Robert
    Robert almost 7 years
    Yes. Or add the command before the line that redefine the user
  • Folyd
    Folyd over 6 years
    The best volume mapping is -v /usr/local/bin:/user/bin, otherwise will cause :docker: Error response from daemon: Mounts denied: EOF.
  • Guillaume Husta
    Guillaume Husta almost 6 years
    Is it still recommended ? See this note : 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.. Source : jpetazzo.github.io/2015/09/03/…
  • François Maturel
    François Maturel almost 6 years
    @Guillaume Husta, you are probably right, if Docker uses system libraries, it may be unsafe. However, I'm using this binding from a coreos host, in a jenkins debian image since 1 year now in 30+ docker images... Not seen any conflict or trouble in a docker build process.
  • Guillaume Husta
    Guillaume Husta almost 6 years
    OK thanks for your feedback. As you said, it will depend on the context.
  • ChernikovP
    ChernikovP almost 5 years
    Does this approach require extra configuration? I've done the same thing and was able to run simple commands as docker -v, but when it comes to docker build I got Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?... Any advice?
  • mirekphd
    mirekphd over 4 years
    I think the original answer is still valid, as this way of exposing host's docker binary using volume mount is exactly what is done under Docker Build strategy by Red Hat's Openshift Container Platform v3.11 (LTS) (you can clearly see /var/run/docker.sock listed in Volume Mounts created by the service account), so I hope this alleviates some concerns regarding stability ;)
  • madhu131313
    madhu131313 over 3 years
    getting this error on using the above method docker: Error response from daemon: Unable to find group docker.
  • khushboo29
    khushboo29 over 3 years
    @ChernikovP I am getting the same error. Could you solve it?
  • Guilherme Abacherli
    Guilherme Abacherli almost 2 years
    @ChernikovP, @khushboo29 you need to add jenkins to group docker, something this way: sudo usermod -a -G docker jenkins