Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Docker Buildx: Multi-Architecture Image Building with Enhanced Cache Control

Tech May 13 2

Multi-Architecture Builds

Buildx leverages QEMU emulation and Docker’s BuildKit engine to compile images for architectures like AMD64, ARM64, and others without requiring physical hardware. This allows a single build command to produce images compatible with diverse environments—from x86 servers to Raspberry Pi devices—pushed under a unified tag to registries like Docker Hub or private repositories. #### Parallel Build Execution

By default, Buildx utilizes BuildKit’s optimized build graph engine to process build stages in parallel. This reduces build times significantly, especially for complex Dockerfiles with multiple layers, by efficiently utilizing available CPU cores and avoiding redundant operations. #### Advanced Cache Management

Unlike traditional Docker builds, Buildx supports persistent and distributed caching mechanisms. Caches can be exported to external storage (e.g., registry, local filesystem) and imported across different build agents, ensuring consistent build performance in CI systems and enabling cache sharing between development and production pipelines. ### Installation

Prerequisite: Install Docker Engine

Ensure Docker is installed and running. Below are platform-specific installation steps. Ubuntu:``` sudo apt update sudo apt install apt-transport-https ca-certificates curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io sudo systemctl enable --now docker


**CentOS/RHEL:**```
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
sudo systemctl enable --now docker

Install Buildx as a CLI Plugin

Buildx is distributed as a standalone plugin. Install it via the following steps: ``` mkdir -p ~/.docker/cli-plugins curl -SL https://github.com/docker/buildx/releases/latest/download/buildx-v0.13.0.linux-amd64 -o ~/.docker/cli-plugins/docker-buildx chmod +x ~/.docker/cli-plugins/docker-buildx


> Note: Replace `v0.13.0` with the latest version from the [official releases page](https://github.com/docker/buildx/releases). #### Verify Installation

Run the following to confirm Buildx is accessible: ```
docker buildx version

Expected output includes a version string such as v0.13.0, confirming successful installation. ### Usage Examples

Create and Activate a Builder Instance

Buildx operates through builder instances. Initialize a new one with multi-platform support: ``` docker buildx create --name mybuilder --use docker buildx inspect --bootstrap


The `--bootstrap` flag initializes the builder with necessary backend components, including QEMU emulators for cross-platform builds. #### Build for Multiple Architectures

To build a single image targeting both AMD64 and ARM64: ```
docker buildx build --platform linux/amd64,linux/arm64 -t myapp:latest --push .

This command: - Compiles the image for both platforms concurrently - Tags it as myapp:latest- Pushes the resulting manifest list to the configured registry #### Export and Reuse Build Cache

To persist build layers across CI runs: Export cache to registry:``` docker buildx build --platform linux/amd64,linux/arm64
--cache-to=type=registry,ref=myregistry.example.com/cache/myapp:latest
--cache-from=type=registry,ref=myregistry.example.com/cache/myapp:latest
-t myapp:latest --push .


**Export cache to local filesystem:**```
docker buildx build --build-arg BUILDKIT_INLINE_CACHE=1 \
  --cache-to=type=local,dest=/tmp/build-cache \
  -t myapp:latest .

Import from local cache:``` docker buildx build --cache-from=type=local,src=/tmp/build-cache
-t myapp:latest .


This cache strategy dramatically accelerates rebuilds in CI pipelines by reusing previously built layers, even when source code changes minimally. </div>

Related Articles

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.