0
votes

I have created GKE cluster and install Jenkins on that cluster. Now i am running pipeline, i have Jenkinsfile created which is used to build DockerImage but when i am running the pipeline, it throws exception that Docker not found

1) Created GKE Cluster 2) Installed Jenkins 3) Added Docker hub credentials 4) Added access key for gitlab

Jenkinsfile:

stage('Build Docker Image') { when { branch 'master' } steps { script { echo 'Before docker run' sh 'docker --version' app = docker.build("sarab321/test-pipeline") echo 'docker run successfully' } } }

Please see the exception below

apiVersion: "v1" kind: "Pod" metadata: annotations: {} labels: jenkins: "slave" jenkins/cd-jenkins-slave: "true" name: "default-d7qdb" spec: containers: - args: - "59c323186a77b4be015362977ec64e4838001b6d77c0f372bec7cda7cf93f9b2" - "default-d7qdb" env: - name: "JENKINS_SECRET" value: "59c323186a77b4be015362977ec64e4838001b6d77c0f372bec7cda7cf93f9b2" - name: "JENKINS_TUNNEL" value: "cd-jenkins-agent:50000" - name: "JENKINS_AGENT_NAME" value: "default-d7qdb" - name: "JENKINS_NAME" value: "default-d7qdb" - name: "JENKINS_URL" value: "http://cd-jenkins.default.svc.cluster.local:8080" image: "jenkins/jnlp-slave:3.27-1" imagePullPolicy: "IfNotPresent" name: "jnlp" resources: limits: memory: "512Mi" cpu: "1" requests: memory: "256Mi" cpu: "500m" securityContext: privileged: false tty: false volumeMounts: - mountPath: "/var/run/docker.sock" name: "volume-0" readOnly: false - mountPath: "/home/jenkins" name: "workspace-volume" readOnly: false workingDir: "/home/jenkins" nodeSelector: {} restartPolicy: "Never" serviceAccount: "default" volumes: - hostPath: path: "/var/run/docker.sock" name: "volume-0" - emptyDir: medium: "" name: "workspace-volume"

  • docker version /home/jenkins/workspace/TestPipeline_master@tmp/durable-5dd73d2b/script.sh: 1: /home/jenkins/workspace/TestPipeline_master@tmp/durable-5dd73d2b/script.sh: docker: not found
1

1 Answers

1
votes

Doesn't look like docker is installed on your build agent, that's inside the container using the "jenkins/jnlp-slave:3.27-1" image. I have examples of how I've installed the docker CLI in the jenkins LTS image at: https://github.com/sudo-bmitch/jenkins-docker

That image includes the following steps to make the docker integration portable:

  • installs the Docker CLI
  • installs gosu (needed since the entrypoint will start as root)
  • configures the jenkins user to be a member of the docker group
  • includes an entrypoint that adjusts to docker GID to match that of the /var/run/docker.sock GID

The actual docker CLI install is performed in the following lines:

RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - \
 && add-apt-repository \
     "deb [arch=amd64] https://download.docker.com/linux/debian \
     $(lsb_release -cs) \
     stable" \
 && apt-get update \
 && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
    docker-ce-cli${DOCKER_CLI_VERSION}

You can take the entrypoint.sh and Dockerfile, modify the base image (FROM) of the Dockerfile, and the original entrypoint script inside entrypoint.sh, to point to the jnlp-slave equivalents.