Docker: not found in Jenkins pipeline

@leoaref - I am stuck with the same problem that you reported back in Apr '17, docker: not found. Did you ever manage to resolve your issue?

I am able to run jenkins pipeline fine in ubuntu. but the same configuration and pipeline does not work in RHEL 7.3.

If you have found a resolution to you issue, I will be grateful if you could respond with the solution

Solution is to install docker in your jenkins container.

Anyone interested to find out more may want to take a look at https://github.com/shazChaudhry/docker-jenkins/blob/ee0f386fd1706829b956cb2e723c0f2935496933/Dockerfile

@jpetazzo is it possible to implement the side car pattern from a jenkins pipeline script where jenkins itself is installed on a container i.e. can the agent being instantiated from the pipeline script run from the host machine? Per your post, I would rather stay away from running docker inside a docker container.

Are there any example of installing the Docker CLI using your base image’s packaging system, or using the Docker API?

Edit: Never mind, that’s literally what you explain in the article thanks!

Yes, I found the solution. Following command is working fine for me.

docker run --rm -u root -p 8080:8080 -v jenkins-data:/var/jenkins_home -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -v "$HOME":/home jenkinsci/blueocean
5 Likes

it doesn’t work with me ,image . exec the command in docker container

You should not mount the binary as mentioned in the answer above but instead install the docker client in your container.
Reason is that the binary is linking against a shared library which you would need to mount as well into the container. But doing that would result in other issues (eg. wrong system dependencies).

Please have a look as well here: http://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/

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.

Thanks is worked for me i just added this

 -v $(which docker):/usr/bin/docker
1 Like

docker run --name jenkins -p 8080:8080 -p 50000:50000
-v /var/run/docker.sock:/var/run/docker.sock
-v $(which docker):/usr/bin/docker
-v /home/jenkins_home:/var/jenkins_home
jenkins/jenkins

1 Like

The following worked perfectly for me:

Basically the solution is to build a docker container which will build upon jenkins and install docker into it, at the end its basically jenkins with the docker client (not server) installed, and when running it you connect it to the host docker.sock as was mentioned above as well

1 Like

This blog, which I wrote, has plenty of info on the problems with running Jenkins pipelines with Docker.

I recently founded a company called Nestybox that has created a container runtime (aka runc) that enables Docker to deploy containers that act like virtual hosts and can run system-level software such as Docker in them, without using privileged containers. It solves many of the issues described by Jerome in his docker-in-docker blog post (see Jerome’s comment above).

We have a solution that runs Jenkins + Docker inside the system container, avoids many of the problems listed in this thread, and does so in complete isolation from the Docker on the host. Check it out, hopefully it will help!

2 Likes

Awesome stuff! This worked perfectly for me! I simply spun up a ubuntu 19.04 VM, and followed the Sysbox Installation Guide and @ctalledo’s blog post and was able to get Docker working in Jenkins pipelines (running in a dockerized Jenkins installation). More details here.

I meet same issue, it fixed mine. Thanks.

this worked for me too.

docker run -u 0 --privileged --name jenkins -it -d -p 8080:8080 -p 50000:50000 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker \
-v /home/jenkins_home:/var/jenkins_home \
jenkins/jenkins:latest
1 Like

Although using “–privileged” flag while starting docker container docker run -u 0 --privileged --name jenkins ... may allow enabling docker in Jenkins pipeline, there are certain implications of starting docker in privileged mode

tanx more need this work for me …
خرید گیفت کارت

This solution worked for me. Thank you!

docker run -u 0 --privileged --name jenkins -d -p 8080:8080 -p 50000:50000 -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

worked for me.

docker run -d -u root -v /var/jenkins_home:/var/jenkins_home -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -p 8081:8080 -p 5001:5000 --name jenkins-docker --privileged jenkins/jenkins:lts

none of these are working for me :confused:
I’ve tried -u root and --group-add 0, --privileged too. $(which docker) is not working inside the docker command buts works separately so I’ve replaced $(which docker) with the $(which docker) literal which is something like C:/Program Files/..../bin/docker.
I’ve also tried docker on docker but not working.
I’d be happy if I’ve got a separate error. But Jenkins is giving me

/usr/bin/docker: 5: /usr/bin/docker: Cannot fork

because it cannot identify docker after all this. help :frowning:

but how to resolve this at host level,as my jenkins pod running in kubernetes and facing the same issue,if any body can help me with the deployment.yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins-app
labels:
 app: jenkins-app
spec:
replicas: 1
selector:
 matchLabels:
   app: jenkins-app
template:
 metadata:
   labels:
     app: jenkins-app
 spec:
   securityContext:
     runAsUser: 1000
     runAsGroup: 3000
     fsGroup: 1234
   containers:
   - name: jenkins-app
     image: jenkins/jenkins:latest
     imagePullPolicy: Always
     ports:
     - name: http-port
       containerPort: 8080
     - name: jnlp-port
       containerPort: 50000
     volumeMounts:
       - name: jenkins-home
         mountPath: /var
         subPath: jenkins_home
       - name: docker-sock
         mountPath: /var/run/docker.sock
     securityContext:
         allowPrivilegeEscalation: false
   volumes:
   - name: jenkins-home
     persistentVolumeClaim:
       claimName: jenkins-pv-claim
   - name: docker-sock
     hostPath:
       path: /var/run/docker.sock
   dnsPolicy: ClusterFirst
---
apiVersion: v1
kind: Service
metadata:
name: jenkins-app-service
labels:
 app: jenkins-app-service
spec:
selector:
 app: jenkins-app
type: LoadBalancer  
ports:
- port: 8080
 protocol: TCP
 targetPort: 8080
 nodePort: 30003
---
apiVersion: v1
kind: Service
metadata:
name: jenkins-jnlp-service
labels:
 app: jenkins-jnlp-service
spec:
selector:
 app: jenkins-app
type: ClusterIP  
ports:
- port: 50000
 protocol: TCP
 targetPort: 50000  
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: jenkins-pv-volume
annotations:
 pv.beta.kubernetes.io/gid: "1234"
labels:
 type: local
spec:
storageClassName: manual
capacity:
 storage: 6Gi
accessModes:
 - ReadWriteOnce
hostPath:
 path: "/var/jenkins_home"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-pv-claim
spec:
storageClassName: manual
accessModes:
 - ReadWriteOnce
resources:
 requests:
   storage: 3Gi