Docker Exec Format Error: In-depth Analysis and Solutions for Architecture Mismatch Issues

Nov 26, 2025 · Programming · 25 views · 7.8

Keywords: Docker | Architecture Mismatch | exec format error | GitLab CI | Multi-platform Build

Abstract: This article provides a comprehensive analysis of the common 'exec format error' in Docker containers, focusing on the root causes of architecture mismatch problems. Through practical case studies, it demonstrates how to diagnose incompatibility between image architecture and runtime environment, and offers multiple solutions including using docker buildx for multi-architecture builds, setting platform parameters, and adjusting CI/CD configurations. The article combines GitLab CI/CD scenarios to detail the complete process from problem diagnosis to complete resolution, helping developers effectively avoid and solve such cross-platform compatibility issues.

Problem Background and Error Phenomenon

During Docker container deployment, developers often encounter error messages like exec /usr/bin/sh: exec format error. This error typically occurs during container startup, indicating that the system cannot properly execute the specified binary file. From the provided case, the user encountered this problem when running a custom Docker image in GitLab CI/CD pipeline, where the image was built from Ubuntu 20.04 with Python 3 and Robot Framework installed.

The key information from error output shows that GitLab Runner attempted to pull and run the image using Docker executor, but failed during the step_script stage, returning exec /usr/bin/sh: exec format error. This indicates that the shell interpreter inside the container cannot run properly on the current hardware architecture.

Root Cause Analysis

Through in-depth analysis, the core issue lies in the mismatch between Docker image architecture and runtime environment architecture. When inspecting image metadata using docker image inspect command, architecture information similar to the following can be found:

"Architecture": "arm64",
"Variant": "v8",
"Os": "linux"

This indicates that the image was built on an ARM64 architecture system, while the GitLab CI Runner is likely running on an x86_64 architecture system. When binaries of different architectures attempt to execute on incompatible hardware, format errors occur.

From a computer architecture perspective, ARM64 and x86_64 use different Instruction Set Architectures (ISA). ARM64 employs Reduced Instruction Set Computing (RISC), while x86_64 uses Complex Instruction Set Computing (CISC). The machine code of these two architectures is completely incompatible at the binary level, preventing direct cross-execution.

Solutions and Implementation Steps

Method 1: Rebuild Image on Target Architecture

The most straightforward solution is to rebuild the Docker image on a system with the same architecture as the CI Runner. This can be achieved through the following steps:

# Build image on GitLab CI Runner
docker build -t rethkevin/rf:v1 .
docker push rethkevin/rf:v1

This method ensures complete alignment between image architecture and runtime environment, avoiding any compatibility issues. For continuous integration environments, it's recommended to integrate image building steps into the CI/CD pipeline to ensure each deployment uses correctly architected images.

Method 2: Use Docker Buildx for Multi-architecture Images

For scenarios requiring support for multiple architectures, Docker Buildx tool can be used to build multi-platform images. Buildx is Docker's extended build tool that supports building images for multiple platforms simultaneously:

# Create and use buildx builder
docker buildx create --name multiarch --use

# Build image supporting multiple architectures
docker buildx build --platform linux/amd64,linux/arm64 -t rethkevin/rf:v1 . --push

This method is particularly suitable for deployments to mixed-architecture environments. After building, Docker automatically selects matching image layers based on the runtime environment's architecture.

Method 3: Explicitly Specify Platform Parameters

When building or running images, target platform can be explicitly specified to ensure architecture compatibility:

# Use environment variable to specify default platform
export DOCKER_DEFAULT_PLATFORM="linux/amd64"
docker build -t rethkevin/rf:v1 .

# Or use --platform parameter
docker build --platform="linux/amd64" -t rethkevin/rf:v1 .

# Specify base image platform in Dockerfile
FROM --platform=linux/amd64 ubuntu:20.04

This method typically works directly in Docker Desktop environments, as Docker Desktop includes built-in platform emulation capabilities. For non-Desktop environments, additional QEMU emulation support may need to be installed.

GitLab CI/CD Configuration Optimization

For GitLab CI/CD environments, architecture compatibility can be ensured by modifying the .gitlab-ci.yml file:

robot-framework:
  image: rethkevin/rf:v1
  allow_failure: true
  variables:
    DOCKER_DEFAULT_PLATFORM: "linux/amd64"
  script:
    - ls
    - pip3 --version

Alternatively, a better approach is to include image building steps in the CI pipeline, ensuring each deployment uses the latest and correctly architected image:

stages:
  - build
  - test

build-image:
  stage: build
  script:
    - docker build --platform="linux/amd64" -t rethkevin/rf:v1 .
    - docker push rethkevin/rf:v1

robot-framework:
  stage: test
  image: rethkevin/rf:v1
  script:
    - ls
    - pip3 --version

Related Cases and Extended Analysis

The similar problem described in the reference article further confirms that architecture mismatch is a common cause of exec format error. In that case, the user encountered the same issue of entrypoint script execution failure when running an image built for x86_64 architecture on Raspberry Pi 4 (ARM64 architecture).

The prevalence of this problem stems from the heterogeneity of modern development environments. Developers may switch between personal computers (typically x86_64), cloud servers (various architectures), and edge devices (like Raspberry Pi, ARM architecture). Without attention to architecture compatibility, such problems easily occur.

Preventive Measures and Best Practices

To avoid similar architecture compatibility issues, the following preventive measures are recommended:

  1. Explicitly specify base image platform in Dockerfile
  2. Integrate multi-architecture image building in CI/CD pipelines
  3. Regularly check image architecture using docker image inspect
  4. Establish architecture compatibility check processes within teams
  5. Consider using base images that support multiple architectures, such as ubuntu:20.04 itself supports multiple architectures

By implementing these best practices, deployment failures caused by architecture mismatches can be significantly reduced, improving the reliability and portability of containerized applications.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.