Docker exec /usr/bin/sh exec format error
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The exec /usr/bin/sh: exec format error means Docker cannot execute the container's entrypoint binary because it was compiled for a different CPU architecture than the host. This almost always comes down to running an ARM image on an x86_64 machine (or the reverse). The Linux kernel does not care that Docker is involved. If the binary's ELF header declares aarch64 but the host CPU is x86_64, the kernel refuses to load it and returns ENOEXEC, which Docker surfaces as "exec format error."
A less common but equally frustrating cause is a shell script entrypoint that is missing a proper shebang line or was saved with Windows-style line endings.
Diagnosing the Root Cause
Before applying any fix, confirm what is actually mismatched.
Check the host architecture:
Check the image architecture:
Check the image manifest for available platforms:
If the host says x86_64 but the image says arm64, you have found the problem.
Check for shebang issues in entrypoint scripts:
If the first line is not a valid shebang, or if file entrypoint.sh reports "ASCII text, with CRLF line terminators," the script will fail with the same error even on the correct architecture.
Fix 1: Pull the Correct Platform Image
Docker Hub images often publish multi-architecture manifests. You can pull a specific platform explicitly:
This forces Docker to download the amd64 variant regardless of what the host architecture auto-detection would choose. This is the simplest fix when a multi-arch image exists.
Fix 2: Rebuild for the Target Architecture
If you own the Dockerfile, rebuild it for the correct platform:
For broader compatibility, build for multiple architectures using Docker Buildx:
The resulting manifest list allows Docker on any supported platform to pull the correct variant automatically.
Fix 3: Enable QEMU Emulation
When you need to run an image built for a different architecture and cannot rebuild it, QEMU user-mode emulation lets the kernel translate foreign binaries on the fly:
Emulation is slower than native execution (expect 3-10x overhead depending on the workload), but it is invaluable for testing and CI pipelines.
Fix 4: Fix the Entrypoint Script
If the architecture matches but you still get the error, the problem is almost certainly the entrypoint script.
Add a shebang line:
The script must start with a valid shebang:
Fix Windows line endings:
Or add a conversion step in the Dockerfile:
Fix 5: Use the Exec Form for ENTRYPOINT
Docker has two entrypoint formats. The shell form wraps your command in /bin/sh -c, which can mask or cause format errors:
When using the exec form, Docker calls the binary directly without involving /bin/sh. If the binary itself is for the wrong architecture, you get a clearer error. If /bin/sh is the wrong architecture (e.g., in a scratch-based image that has no shell), the shell form fails with exec format error even if your binary is correct.
Common Causes at a Glance
| Cause | Symptom | Fix |
| Architecture mismatch (ARM image on x86) | Error on container start | Pull correct platform or rebuild |
| Architecture mismatch (x86 image on ARM Mac) | Error on container start | docker pull --platform linux/amd64 + Rosetta/QEMU |
| Missing shebang in entrypoint script | Error on container start | Add #!/bin/sh as first line |
| Windows CRLF line endings in script | Error on container start | Convert to LF with sed or .gitattributes |
Shell form ENTRYPOINT with no /bin/sh | Error on container start | Switch to exec form ENTRYPOINT ["/myapp"] |
| Corrupt or incomplete image layers | Error on container start | docker pull again to re-download |
Apple Silicon (M1/M2/M3) Specifics
Apple Silicon Macs run aarch64 (ARM64). Many Docker images are still published only as linux/amd64. Docker Desktop for Mac includes Rosetta 2 emulation, but it must be enabled:
With Rosetta enabled, linux/amd64 images run transparently on ARM Macs. Without it, you either need QEMU emulation (slower) or native ARM images.
When building images on an M1/M2/M3 Mac that will be deployed to x86_64 Linux servers, always specify the platform explicitly:
Otherwise, Docker defaults to linux/arm64, and the image will fail on the target server with the exact error this article describes.
Preventing the Error in CI/CD
In CI/CD pipelines, architecture mismatches often happen silently because the build machine and the deployment target differ. Add a platform check to your pipeline:
Common Pitfalls
Assuming Docker handles architecture differences automatically. Docker pulls the manifest matching your host architecture. If the image only has one variant and it does not match your host, Docker pulls it anyway and fails at runtime, not at pull time.
Forgetting --platform when building on Apple Silicon for Linux deployments. This is the number one cause of this error in 2024-2025 era development. Always set --platform linux/amd64 explicitly when building for x86_64 targets.
Ignoring .gitattributes for line endings. If your repository has shell scripts checked in with autocrlf=true, Git converts LF to CRLF on Windows checkouts. Add *.sh text eol=lf to .gitattributes to prevent this.
Using FROM scratch without understanding the consequences. A scratch-based image has no shell, no filesystem tools, nothing. Any ENTRYPOINT in shell form will fail because /bin/sh does not exist. Always use exec form with scratch.
Not checking docker buildx ls for available builders. If Buildx is not configured, multi-platform builds silently fall back to single-platform. Verify your builder supports the target platforms.
Summary
- The
exec format errormeans the binary's CPU architecture does not match the host kernel, or the entrypoint script is malformed. - Check
uname -mon the host anddocker inspecton the image to confirm the mismatch. - Pull the correct platform with
--platform linux/amd64(orlinux/arm64). - Use Docker Buildx for multi-architecture builds when your image needs to run on multiple platforms.
- Enable QEMU emulation for cross-architecture testing when rebuilding is not an option.
- Check entrypoint scripts for missing shebangs and Windows line endings.
- On Apple Silicon Macs, enable Rosetta in Docker Desktop and always specify
--platformwhen building for x86_64 deployment targets.
Related reading
- docker executable file not found in PATH
- Docker Failed to Initialize on Windows
- Docker follow symlink outside context
- Docker for Desktop runs the Kubernetes - Ip address is not working
- Docker for Windows error Hardware assisted virtualization and data execution protection must be enabled in the BIOS
- Docker for Windows stuck at Kubernetes is Starting after updating to version 2.1.1.0 Edge or Stable
- Docker for Mac - Kubernetes - reference local image
- Docker forever in Docker is starting.. at Windows task

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.