如何使用 kuberntes 代理构建 docker 镜像?

如何使用 kuberntes 代理构建 docker 镜像?

下面是我在kubernetes插件中设置的配置。

图片1

图片2

图片3 图片4

我的詹金斯管道。

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'
            }
        }
    }
}

我收到的错误:

[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

我做了一些研究,并改变了管道步骤部分。

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

但仍然出现此错误。

> + 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?

相关内容