Score:0

How to build a docker image using kuberntes agent?

kr flag

Below is the configuration in kubernetes plugin I set.

img1

img2

img3 img4

My jenkins pipeline.

pipeline {
    agent {
        kubernetes {
            label 'my-docker-build'
            defaultContainer 'jnlp'
            yaml """
            apiVersion: v1
            kind: Pod
            metadata:
              labels:
                app: my-docker-build
            spec:
              containers:
              - name: docker
                image: docker:latest
                command: ["cat"]
                tty: true
              volumes:
              - name: docker-sock
                hostPath:
                  path: /var/run/docker.sock
              - name: my-repo
                gitRepo:
                  repository: https://github.com/my/repo.git
            """
        }
    }
    stages {
        stage('Build Docker Image') {
            steps {
                sh 'hostname'
                sh 'sleep 50'
                sh 'docker --version'
                sh 'docker build -t my-docker-image:latest .'
            }
        }
        stage('Push Docker Image') {
            steps {
                sh 'docker push my-docker-image:latest'
            }
        }
    }
}

The error I am getting:

[Pipeline] Start of Pipeline
[Pipeline] echo (hide)
[WARNING] label option is deprecated. To use a static pod template, use the 'inheritFrom' option.
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Created Pod: kubernetes jenkins/my-docker-build-md4d4-jfzc3
Agent my-docker-build-md4d4-jfzc3 is provisioned from template my-docker-build-md4d4
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    buildUrl: "http://jenkins-service.jenkins.svc.cluster.local:8080/job/testdockerbuildchatgpt/4/"
    runUrl: "job/testdockerbuildchatgpt/4/"
  labels:
    app: "my-docker-build"
    jenkins: "agent"
    jenkins/label-digest: "b5593a0e01711fc2ff02061aa718a7559c6e2f44"
    jenkins/label: "my-docker-build"
  name: "my-docker-build-md4d4-jfzc3"
  namespace: "jenkins"
spec:
  containers:
  - command:
    - "cat"
    image: "docker:latest"
    name: "docker"
    tty: true
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  - env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_AGENT_NAME"
      value: "my-docker-build-md4d4-jfzc3"
    - name: "JENKINS_NAME"
      value: "my-docker-build-md4d4-jfzc3"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://jenkins-service.jenkins.svc.cluster.local:8080/"
    image: "jenkins/inbound-agent:3107.v665000b_51092-4"
    name: "jnlp"
    resources:
      requests:
        memory: "256Mi"
        cpu: "100m"
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  volumes:
  - hostPath:
      path: "/var/run/docker.sock"
    name: "docker-sock"
  - gitRepo:
      repository: "https://github.com/my/repo.git"
    name: "my-repo"
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Running on my-docker-build-md4d4-jfzc3 in 

    /home/jenkins/agent/workspace/testdockerbuildchatgpt
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Build Docker Image)
    [Pipeline] sh
    + hostname
    my-docker-build-md4d4-jfzc3
    [Pipeline] sh
    + sleep 50
    [Pipeline] sh
    + docker --version
    /home/jenkins/agent/workspace/testdockerbuildchatgpt@tmp/durable-06779b6e/script.sh: 1: docker: not found
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Push Docker Image)
    Stage "Push Docker Image" skipped due to earlier failure(s)
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] }
    [Pipeline] // podTemplate
    [Pipeline] End of Pipeline
    ERROR: script returned exit code 127
    Finished: FAILURE

I did some research and changed the pipeline step with this part.

stage('Build Docker Image') {
            steps {
                container('docker') {
                sh 'hostname'
                sh 'sleep 50'
                sh 'docker --version'
                sh 'docker build -t my-docker-image:latest .'
                }}}

But still getting this error.

> + docker build -t my-docker-image:latest . ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon
> running?
Sai Chandini Routhu avatar
dk flag
Refer this [blog](https://techoverflow.net/2018/12/15/how-to-fix-docker-cannot-connect-to-the-docker-daemon-at-unix-var-run-docker-sock-is-the-docker-daemon-running-on-ubuntu/) helpful to resolve your issue
kr flag
I have included docker-sock volume already as in the blog you posted. As you can see in the question text. Could you please check if any other options.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.