CloudSen
CloudSen

Reputation: 113

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

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 ?

Upvotes: 3

Views: 12619

Answers (3)

tgogos
tgogos

Reputation: 25240

Make sure that buildx has been installed to your Jenkins agent that triggers the unknown flag: --platform error.

This is where you have to go back in time a bit šŸ˜€ and remember the way you installed docker... For example, for my case, where I had:

  • an Ubuntu 22.04 instance and
  • Docker installed from the Ubuntu Packages (sudo apt install docker.io)

The solution that worked for me, was just running the following command:

sudo apt install docker-buildx

Another place to look at (in case you used these installation steps) is the instructions for setting up and using the official Docker repositories: Install (Docker Engine on Ubuntu) using the apt repository

In this case, the relative package has a different name:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
                                                           ^^^^^^^^^^^^^^^^^^^^

Upvotes: 1

MightGod
MightGod

Reputation: 474

If someone happens to encounter this problem, the solution for me was to install docker desktop from the official site.

I have MacBook Pro with Intel chip, and I installed docker and docker-buildx using Homebrew. When I ran the command docker buildx build --platform=linux/amd64 command I got the same error unknown flag: --platform.

As I mentioned, installing docker desktop solved this issue.

Upvotes: 0

Val Kalinichenko
Val Kalinichenko

Reputation: 351

Make sure your Jenkins agent (slave) has recent version of docker. BuildKit has been integrated toĀ docker buildĀ since Docker 18.06 .Ā 

In my case version 18.09.6 did not work. 20.10 is good though.

Upvotes: 0

Related Questions