<p>I'm new to Dev Ops and trying to build my code using Jenkins and upload it on the kubernetes cluster which is hosted on the IBM cloud. But when I run the Docker run command in the Jenkins script I keep getting this error. Installed all the latest plugins and</p>
<pre><code>+ docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
</code></pre>
<p>Here's the Jenkins script which I don't know is right or wrong. I searched a couple of articles and question. They all were not giving me a positive result.
Tried this <a href="https://stackoverflow.com/questions/37647251/jenkins-docker-in-docker-on-gcp-kubernetes">Jenkins Docker in Docker on GCP/Kubernetes</a>.</p>
<pre><code>podTemplate(
    cloud: &quot;kubernetes&quot;,
    label:&quot;mypod&quot;,
    containers:[
        containerTemplate(
            name:&quot;nodejs&quot;,
            image:&quot;node&quot;,
            ttyEnabled:true,
            command:'cat',
            alwaysPullImage: true,
            resourceRequestCpu: '200m',
            resourceRequestMemory: '100Mi',
        ),
        containerTemplate(
            name:&quot;docker&quot;,
            image:&quot;&quot;,
            ttyEnabled:true,
            command:'cat',
            alwaysPullImage: true,
            resourceRequestCpu: '200m',
            resourceRequestMemory: '100Mi',
        ),
        containerTemplate(
            name:&quot;helm&quot;,
            image:&quot;alpine/helm&quot;,
            ttyEnabled:true,
            command:'cat',
            alwaysPullImage: true,
            resourceRequestCpu: '200m',
            resourceRequestMemory: '100Mi',
        )
    ],
    volumes:[
        hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')
    ]
){
    node(&quot;mypod&quot;){
        def commitId
        stage (&quot;Fetch repo&quot;){
            checkout scm
            commitId = sh(script: 'git rev-parse --short HEAD',returnStdout:true).trim()
        }
        stage (&quot;Installing packages&quot;){
            container(&quot;nodejs&quot;){
                sh 'npm install'
            }
        }
        stage (&quot;Build&quot;){
            container(&quot;nodejs&quot;){
                sh 'npm run build'
            }
        }
        def repository
        stage (&quot;Docker&quot;){
            container('docker'){
                docker.withRegistry(&quot;https://us.icr.io/api&quot;,&quot;ibm-cloud&quot;){
                    sh &quot;docker run hello-world&quot;
                }
            }
        }
        stage (&quot;Deploy&quot;){
            container (&quot;helm&quot;){
                sh 'helm version'
            }
        }
    }
}
</code></pre>
<p>This is the deployment file of my Jenkins pod</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-uat
  labels:
    app: jenkins
    chart: jenkins-5.0.18
    release: jenkins-uat
    heritage: Helm
spec:
  selector:
    matchLabels:
      app: jenkins
      release: jenkins-uat
  template:
    metadata:
      labels:
        app: jenkins
        chart: jenkins-5.0.18
        release: jenkins-uat
        heritage: Helm
    spec:      
      securityContext:
        fsGroup: 1001
      containers:
        - name: jenkins
          image: docker.io/bitnami/jenkins:2.235.1-debian-10-r7
          imagePullPolicy: &quot;IfNotPresent&quot;
          securityContext:
            runAsUser: 1001
          env:
            - name: JENKINS_USERNAME
              value: &quot;hlpjenkin&quot;
            - name: JENKINS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: jenkins-uat
                  key: jenkins-password
            - name: JENKINS_HOME
              value: &quot;/opt/bitnami/jenkins/jenkins_home&quot;
            - name: DISABLE_JENKINS_INITIALIZATION
              value: &quot;no&quot;
          ports:
            - name: http
              containerPort: 8080
            - name: https
              containerPort: 8443
          livenessProbe:
            httpGet:
              path: /login
              port: http
            initialDelaySeconds: 180
            periodSeconds: 10
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 6
          readinessProbe:
            httpGet:
              path: /login
              port: http
            initialDelaySeconds: 30
            periodSeconds: 5
            timeoutSeconds: 3
            successThreshold: 1
            failureThreshold: 3
          resources:
            limits: {}
            requests:
              cpu: 300m
              memory: 512Mi
          volumeMounts:
            - name: jenkins-data
              mountPath: /bitnami/jenkins
      volumes:
        - name: jenkins-data
          persistentVolumeClaim:
            claimName: jenkins-uat
</code></pre>

Farrukh Zeb
<p>So I have installed Jenkins as a container in my k8s cluster :) and managed to <strong>reproduce</strong> the same error:</p>
<pre><code>docker run --rm hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.
</code></pre>
<p><strong>How to fix it</strong>.</p>
<p>In order to fix you definitely need to have access to the Docker on your K8s Node. Very good explanation of how that works was given by <a href="https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/" rel="nofollow noreferrer">jpetazzo</a>.</p>
<p>Technically you do not need &quot;Docker in Docker&quot; (that is the &quot;full Docker setup&quot; in Docker). You just want to be able to run Docker from your CI system, while this CI system itself is in a container. So that that your CI system like Jenkins can start containers.</p>
<p>So when you start your CI container (Jenkins or other), instead of hacking something together with Docker-in-Docker, start it with the access to <code>/var/run/docker.sock</code> on main host.</p>
<p>Below you can see the part of my <a href="https://github.com/nkolchenko/innovative-solutions/tree/master/6345183-jenkins-docker" rel="nofollow noreferrer">Yamls</a> that a responsible for that.<br />
That allows my CI container to have access to the Docker socket, and CI container will, therefore, be able to start containers.</p>
<p>Except that instead of starting “child” containers, it will start “sibling” containers, but that is perfectly fine in our context.</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
...
spec:
  template:
    spec:
      containers:
      - env:
        volumeMounts:
        - mountPath: /var/run/docker.sock
          name: docker-sock
      ...
      volumes:
      - hostPath:
          path: /var/run/docker.sock
          type: File
        name: docker-sock
</code></pre>
<p>So in my case, the pipeline I've created produces the following logs:</p>
<pre><code>####pipeline

pipeline {
    agent any

    stages     {
        stage('second_stage'){
            steps{
                sh 'docker run --rm hello-world'
            }
        }
    }
}

####logs

+ docker run --rm hello-world

Hello from Docker!
</code></pre>
