<p>Using Jenkins on Kubernetes plugin and using Jenkins as a code.</p>
<p>I'm getting this error when trying to use 'docker build'</p>
<p><code>Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?</code></p>
<ol>
<li>I tried to mount /var/run/docker.sock.. but still not working..</li>
<li>I tried to use runAsUser: root to run with root permissions... but still not working..</li>
</ol>
<p>My Jenkins as a code pod template configuration -</p>
<pre><code>  Jenkins:config:
    chart: jenkins
    namespace: default
    repo: https://charts.jenkins.io
    values:
      agent:
        enabled: true
        podTemplates:
          jenkins-slave-pod: |
            - name: jenkins-xxx-pod
              label: ecs-slave
              serviceAccount: jenkins-xxx-prod
              containers:
                - name: main
                  image: '805xxxx.dkr.ecr.us-west-2.amazonaws.com/slave:ecs-xxxx-node_master-3'
                  command: &quot;sleep&quot;
                  args: &quot;30d&quot;
                  privileged: true
                  runAsUser: root
              volumes:
                - hostPathVolume:
                    hostPath: &quot;/var/run/docker.sock&quot;
                    mountPath: &quot;/var/run/docker.sock&quot;
</code></pre>

EilonA
<p>I assume that you are using k8s &gt;= v1.24 where <code>docker</code> as runtime is not <a href="https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.24.md#dockershim-removed-from-kubelet" rel="nofollow noreferrer">supported anymore</a>.</p>
<p>I would also add that mounting <code>docker</code> socket is not a good practice from security perspective.</p>
<p>If you want to build container image in k8s please use <a href="https://podman.io/" rel="nofollow noreferrer">podman</a> or <a href="https://github.com/GoogleContainerTools/kaniko" rel="nofollow noreferrer">kaniko</a>.</p>
