Answers: <p>The Jenkins <a href="https://plugins.jenkins.io/docker-workflow/" rel="nofollow noreferrer">Docker Pipeline</a> plugin does not support <code>buildx</code> (<a href="https://issues.jenkins.io/browse/JENKINS-61372" rel="nofollow noreferrer">yet?</a>). Voting may help, so add yours to the issue if so inclined.</p>
  <p>Jeremie Drouet wrote a great post &quot;<a href="https://www.docker.com/blog/multi-arch-build-and-images-the-simple-way/" rel="nofollow noreferrer">Multi-arch build and images, the simple way</a>&quot; which covers the underlying ways you can do it by building images first, then create a manifest to link them together.</p>
  <p>bgulla on Github <a href="https://gist.github.com/bgulla/194736124c3824d54d8082918930b1bb" rel="nofollow noreferrer">has a gist</a> that does as much of the build as possible with pipeline using <code>docker.withRegistry()</code>. Then uses <code>sh</code> to create and push the manifest.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-11-01T17:34:37.550Z
Answers: 2022-11-01T17:34:37.550Z
Answers: 8None1
Answers: <p>Here's how to do it in Jenkins only using the plugin for image naming:</p>
  <pre><code>  script {
      docker.withRegistry('my.registry.com', 'my_creds') {
        def image = docker.image(&quot;my_image:my_tag&quot;)
        sh &quot;docker buildx create --use --name multiarch&quot;
        sh &quot;&quot;&quot;
        docker buildx build \
          --platform linux/amd64,linux/arm64 \
          -t ${image.imageName()} \
          --push .
        &quot;&quot;&quot;
      }
    }
  </code></pre>
  <p>For this to work the Jenkins agent needs to have the QEMU binary formats registered with binfmt so the builder it creates will have all the emulators it needs. This docker container will do it:</p>
  <pre><code>docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
  </code></pre>
  <p>This command needs to be be run on every reboot of the agent. In AWS EC-2 you can put it in an executable script in <code>/var/lib/cloud/scripts/per-boot/</code> and see the output in <code>/var/log/cloud-init-output.log</code>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-12-04T01:11:30.480Z
Answers: 2023-12-04T01:11:30.480Z
Answers: Noumenon
Body: <p>I'm currently building my docker image declaratively in a <code>Jenkinsfile</code> by:</p>
  <pre><code>app = docker.build(&quot;...&quot;)
  </code></pre>
  <p>Now I need to generate a platform specific build for ARM64.
  For this purpose</p>
  <pre><code>docker buildx build --platform linux/arm64 ...
  </code></pre>
  <p>must be used.</p>
  <p>I there a way to keep using the <code>docker.build()</code> syntax and still generate a platform specific image?</p>
  <p>Or do I have to rewrite the syntax, if then how?</p>
  
Tags: <docker><jenkins>
Title: Use buildx in Jenkinsfile for platform specific build
UserDisplayName: Sam
ThreadText: # Use buildx in Jenkinsfile for platform specific build
  
  (Asked by: username_0 on 2022-01-18)
  
  I'm currently building my docker image declaratively in a `Jenkinsfile` by:
  
  ```
  app = docker.build("...")
  
  ```
  
  Now I need to generate a platform specific build for ARM64.
  For this purpose
  
  ```
  docker buildx build --platform linux/arm64 ...
  
  ```
  
  must be used.
  
  I there a way to keep using the `docker.build()` syntax and still generate a platform specific image?
  
  Or do I have to rewrite the syntax, if then how?
  
  **Tags:** docker, jenkins
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-11-01. Score: 2
  
  The Jenkins [Docker Pipeline](https://plugins.jenkins.io/docker-workflow/) plugin does not support `buildx` ([yet?](https://issues.jenkins.io/browse/JENKINS-61372)). Voting may help, so add yours to the issue if so inclined.
  
  Jeremie Drouet wrote a great post "[Multi-arch build and images, the simple way](https://www.docker.com/blog/multi-arch-build-and-images-the-simple-way/)" which covers the underlying ways you can do it by building images first, then create a manifest to link them together.
  
  bgulla on Github [has a gist](https://gist.github.com/bgulla/194736124c3824d54d8082918930b1bb) that does as much of the build as possible with pipeline using `docker.withRegistry()`. Then uses `sh` to create and push the manifest.
  
  
  ---
  
  ### Answer by username_2 on 2023-12-04. Score: 0
  
  Here's how to do it in Jenkins only using the plugin for image naming:
  
  ```
    script {
      docker.withRegistry('my.registry.com', 'my_creds') {
        def image = docker.image("my_image:my_tag")
        sh "docker buildx create --use --name multiarch"
        sh """
        docker buildx build \
          --platform linux/amd64,linux/arm64 \
          -t ${image.imageName()} \
          --push .
        """
      }
    }
  
  ```
  
  For this to work the Jenkins agent needs to have the QEMU binary formats registered with binfmt so the builder it creates will have all the emulators it needs. This docker container will do it:
  
  ```
  docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
  
  ```
  
  This command needs to be be run on every reboot of the agent. In AWS EC-2 you can put it in an executable script in `/var/lib/cloud/scripts/per-boot/` and see the output in `/var/log/cloud-init-output.log`.
  
  
  ---
  
  