Creating a Kubernetes deployment that contains Docker and Jenkins

4/8/2021

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 jenkins-docker-in-docker deployment:

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

With a simple Pipeline copied from https://www.jenkins.io/doc/tutorials/build-a-java-app-with-maven/ :

pipeline {
    agent {
        docker {
            image 'maven:3-alpine' 
            args '-v /root/.m2:/root/.m2' 
        }
    }
    stages {
        stage('Build') { 
            steps {
                sh 'mvn -B -DskipTests clean package' 
            }
        }
    }
}

When I attempt to build I receive this error:

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

My understanding which now seems incorrect is that the containers dind-daemon, docker-cmds & ml-services containers exist within the same deployment, in this case named jenkins-docker-in-docker. Therefore, Docker and Jenkins should be available on jenkins-docker-in-docker?

Have I configured the Kubernetes file incorrectly?

Update:

I modified the yaml to set the DOCKER_HOST environment variable:

- name: ml-services
  env:
    - name: DOCKER_HOST
      value: "dind-daemon:2375"
  image: trion/jenkins-docker-client
  ports:
    - containerPort: 8080

The error on Jenkins is now:

  • docker inspect -f . maven:3-alpine

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

Do I need to expose the docker daemon on 10.245.20.10:53 ?

Update 2:

With help from accepted answer I got this working using following deployment:

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: {}

I've likely included some redundant configuration in the above .yaml config, it executes a Docker in Docker container with Jenkins and successfully execute's the following Jenkinsfile:

pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
    }
}
-- blue-sky
docker
jenkins
jenkins-pipeline
kubernetes

1 Answer

4/8/2021

Docker tries to use the unix socket to connect to the daemon. This happens, when no DOCKER_HOST environment variable is set.

Use the correct host and port to connect to your docker daemon. In your setup it would be "dind-daemon".

-- Thomas
Source: StackOverflow