<p>I'm attempting to create a Kubernetes deployment that allows building Docker images and Jenkins on the same host. Here is my YAML configuration for the deployment containing Docker in Docker and Jenkins containers within the <code>jenkins-docker-in-docker</code> deployment:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-docker-in-docker
spec:
  selector:
    matchLabels:
      app: jenkins-docker-in-docker
  replicas: 1
  template:
    metadata:
      labels:
        app: jenkins-docker-in-docker
    spec:
      containers:
        - name: dind-daemon
          image: docker:18.01.0-dind
          resources:
            requests:
              cpu: 20m
              memory: 512Mi
          securityContext:
            privileged: true
          volumeMounts:
            - name: docker-graph-storage
              mountPath: /var/lib/docker
        - name: docker-cmds
          image: docker:18.01.0
          command: ['docker', 'run', '-p', '80:80', 'httpd:latest']
          resources:
            requests:
              cpu: 10m
              memory: 256Mi
          env:
            - name: DOCKER_HOST
              value: tcp://localhost:2375
        - name: ml-services
          image: trion/jenkins-docker-client
          ports:
            - containerPort: 8080
</code></pre>
<p>With a simple Pipeline copied from <a href="https://www.jenkins.io/doc/tutorials/build-a-java-app-with-maven/" rel="nofollow noreferrer">https://www.jenkins.io/doc/tutorials/build-a-java-app-with-maven/</a> :</p>
<pre><code>pipeline {
    agent {
        docker {
            image 'maven:3-alpine' 
            args '-v /root/.m2:/root/.m2' 
        }
    }
    stages {
        stage('Build') { 
            steps {
                sh 'mvn -B -DskipTests clean package' 
            }
        }
    }
}
</code></pre>
<p>When I attempt to build I receive this error:</p>
<pre><code>First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . maven:3-alpine

    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    [Pipeline] isUnix
    [Pipeline] sh
    + docker pull maven:3-alpine
    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: script returned exit code 1
    Finished: FAILURE
</code></pre>
<p>My understanding which now seems incorrect is that the containers <code>dind-daemon</code>, <code>docker-cmds</code> &amp; <code>ml-services</code> containers exist within the same deployment, in this case named <code>jenkins-docker-in-docker</code>. Therefore, Docker and Jenkins should be available on <code>jenkins-docker-in-docker</code>?</p>
<p>Have I configured the Kubernetes file incorrectly?</p>
<p>Update:</p>
<p>I modified the yaml to set the DOCKER_HOST environment variable:</p>
<pre><code>- name: ml-services
  env:
    - name: DOCKER_HOST
      value: &quot;dind-daemon:2375&quot;
  image: trion/jenkins-docker-client
  ports:
    - containerPort: 8080
</code></pre>
<p>The error on Jenkins is now:</p>
<blockquote>
<ul>
<li>docker inspect -f . maven:3-alpine</li>
</ul>
<p>error during connect: Get
http://dind-daemon:2375/v1.40/containers/maven:3-alpine/json: dial
tcp: lookup dind-daemon on 10.245.0.10:53: no such host [Pipeline]
isUnix [Pipeline] sh</p>
<ul>
<li>docker pull maven:3-alpine error during connect: Post http://dind-daemon:2375/v1.40/images/create?fromImage=maven&amp;tag=3-alpine:
dial tcp: lookup dind-daemon on 10.245.0.10:53: no such host
[Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node
[Pipeline] End of Pipeline ERROR: script returned exit code 1
Finished: FAILURE</li>
</ul>
</blockquote>
<p>Do I need to expose the docker daemon on 10.245.20.10:53 ?</p>
<p>Update 2:</p>
<p>With help from accepted answer I got this working using following deployment:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-docker-in-docker1.1
spec:
  selector:
    matchLabels:
      app: jenkins-docker-in-docker1.1
  replicas: 1
  template:
    metadata:
      labels:
        app: jenkins-docker-in-docker1.1
    spec:
      containers:
        - name: dind-daemon
          image: docker:18.01.0-dind
          resources:
            requests:
              cpu: 20m
              memory: 512Mi
          securityContext:
            privileged: true
          volumeMounts:
            - name: docker-graph-storage
              mountPath: /var/lib/docker
            - name: jenkins-home
              mountPath: /var/jenkins_home
        - name: docker-cmds
          image: docker:18.01.0
          command: ['docker', 'run', '-p', '80:80', 'httpd:latest']
          resources:
            requests:
              cpu: 10m
              memory: 256Mi
          env:
            - name: DOCKER_HOST
              value: tcp://localhost:2375
        - name: ml-services
          env:
            - name: DOCKER_HOST
              value: tcp://localhost:2375
          image: trion/jenkins-docker-client
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: jenkins-home
              mountPath: /var/jenkins_home

      volumes:
        - name: docker-graph-storage
          emptyDir: {}
        - name: jenkins-home
          emptyDir: {}
</code></pre>
<p>I've likely included some redundant configuration in the above .yaml config, it executes a <code>Docker in Docker</code> container with <code>Jenkins</code> and successfully execute's the following <code>Jenkinsfile</code>:</p>
<pre><code>pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
    }
}
</code></pre>

<p>Docker tries to use the unix socket to connect to the daemon.
This happens, when no DOCKER_HOST environment variable is set.</p>
<p>Use the correct host and port to connect to your docker daemon. In your setup it would be &quot;dind-daemon&quot;.</p>
