"unknown flag: --platform" when use "docker buildx build" in Jenkins pipeline

Viewed 1580

I am using RedHat-7 system. And I want to Jenkins Pipeline to implement Devops.
But when I use docker buildx build feature, Jenkins says "unknown flag: --platform".

I run my Jenkins with docker image:

docker run -d \
    --name jenkins \
    --restart=unless-stopped \
    -u 0 \
    --network jenkins \
    -p 8082:8080 \
    -p 50000:50000 \
    -v /home/ngtl/jenkins-data:/var/jenkins_home \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v $(which docker):/usr/bin/docker \
    -e TZ=Asia/Shanghai \
    -e JAVA_OPTS=-Duser.timezone=Asia/Shanghai \
    jenkins/jenkins:lts-jdk11

and this is my pipeline:

pipeline {
  agent any

  tools {
      maven 'mvn'
  }

  environment {
    DOCKER_CREDENTIALS = credentials('clouds3n-ldap')
  }

  stages {
    stage('Unit Test') {
      steps {
        withMaven(maven: 'mvn') {
          sh 'mvn clean test -Dmaven.test.failure.ignore=false'
        }
      }
    }

    stage('Maven Build') {
      steps {
        withMaven(maven: 'mvn') {
          sh 'mvn package -Dmaven.test.skip -DskipTests'
        }
      }
    }

    stage('Sonar Scan') {
      steps {
        withSonarQubeEnv('sonarqube') {
          withMaven(maven: 'mvn') {
            script {
              def allJob = env.JOB_NAME.tokenize('/') as String[]
              def projectName = allJob[0]
              sh "mvn sonar:sonar -Dsonar.branch.name=${env.GIT_BRANCH} -Dsonar.projectKey=${projectName} -Dsonar.projectName=${projectName} -Dmaven.test.skip -DskipTests"
            }
          }
        }
      }
    }

    stage('Sonar Gate') {
      steps {
        timeout(time: 30, unit: 'MINUTES') {
          waitForQualityGate abortPipeline: true
        }
      }
    }

    stage('Docker Build') {
      steps {
        script {
          def allJob = env.JOB_NAME.tokenize('/') as String[]
          def projectName = allJob[0]
          final noSuffixProjectName = projectName.substring(0, projectName.lastIndexOf('-'))
          sh "echo ${DOCKER_CREDENTIALS_PSW} | docker login -u ${DOCKER_CREDENTIALS_USR} 192.168.2.157:8881 --password-stdin"
          sh "docker buildx build --platform linux/amd64 -t 192.168.2.157:8881/uni/${noSuffixProjectName}:dev-${BUILD_NUMBER} -f ${env.JENKINS_HOME}/k8s-config/docker/BackendDockerfile . --push"
        }
      }
    }

    stage('Maven Deploy') {
      steps {
        withMaven(maven: 'mvn') {
          sh 'mvn deploy -Dmaven.test.skip -DskipTests'
        }
      }
    }

    stage('K8s Apply') {
      steps {
        echo 'not support now, comming soon'
      }
    }
  }

    post {

      always {
        sh 'docker logout 192.168.2.157:8881'
      }

      cleanup {
        cleanWs()
      }

      success {
        echo 'Finished!'
      }
    }
}

When reach "Docker Build" stage, Jenkins will throw error :

Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
         Affected argument(s) used the following variable(s): [DOCKER_CREDENTIALS_PSW]
         See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ echo ****
+ docker login -u **** 192.168.2.157:8881 --password-stdin
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[Pipeline] sh
+ docker buildx build --platform linux/amd64 -t 192.168.2.157:8881/uni/cqu:dev-11 -f /var/jenkins_home/k8s-config/docker/BackendDockerfile . --push
unknown flag: --platform
See 'docker --help'.

Why Jenkins pipleline can not use "--platform" options? How to fix this problem ?

1 Answers
Related