Docker "exec /usr/bin/sh: exec format error": causes and fixes
Codemia Knowledge Hub ·
exec /usr/bin/sh: exec format error means the kernel cannot run the container's entrypoint binary because it was built for a different CPU architecture than the host. Almost always this is an ARM image on an x86_64 machine, or the reverse. Docker is not the one refusing: if the binary's ELF header says aarch64 and the CPU is x86_64, the kernel returns ENOEXEC, and Docker reports it as "exec format error."
A less common cause is a shell script entrypoint with no valid shebang line, or one saved with Windows line endings.
Confirm what is mismatched
Host architecture:
Image architecture:
Platforms available in the image manifest:
If the host reports x86_64 and the image reports arm64, that is the problem.
Shebang in the entrypoint script:
If the first line is not a valid shebang, or file entrypoint.sh reports "ASCII text, with CRLF line terminators," the script fails with the same error even on the right architecture.
Fix 1: Pull the correct platform image
Many Docker Hub images publish multi-architecture manifests. Request a specific platform explicitly:
This downloads the amd64 variant regardless of what auto-detection would pick. It is the simplest fix when a multi-arch image exists.
Fix 2: Rebuild for the target architecture
If you own the Dockerfile, build for the right platform:
To support several architectures, build with Docker Buildx:
The resulting manifest list lets Docker on any supported platform pull the matching variant automatically.
Fix 3: Enable QEMU emulation
When you must run a foreign-architecture image and cannot rebuild it, QEMU user-mode emulation translates the binaries on the fly:
Emulation is slower than native execution (expect 3-10x overhead depending on the workload), but it is very useful for testing and CI.
Fix 4: Repair the entrypoint script
If the architectures match and the error persists, the entrypoint script is the likely culprit.
Make sure it is copied and executable:
The script must start with a valid shebang:
Convert Windows line endings:
Or do the conversion in the Dockerfile:
Fix 5: Use the exec form for ENTRYPOINT
The shell form of ENTRYPOINT wraps your command in /bin/sh -c, which can cause or hide format errors:
The exec form calls the binary directly without /bin/sh. If the binary is the wrong architecture, the error is clearer. If /bin/sh itself is the wrong architecture, the shell form fails with exec format error even when your binary is fine, and in a scratch-based image with no shell at all, the shell form cannot start because /bin/sh does not exist.
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)
Apple Silicon Macs are aarch64 (ARM64), and many images are still published only as linux/amd64. Docker Desktop for Mac can run them through Rosetta 2, but you have to enable it:
With Rosetta on, linux/amd64 images run transparently. Without it, you need QEMU (slower) or native ARM images.
When you build on an M1/M2/M3 Mac for x86_64 Linux servers, always set the platform:
Otherwise Docker builds linux/arm64, and the image fails on the server with exactly this error.
Catching the mismatch in CI/CD
Build machines and deployment targets often differ, so the mismatch can slip through silently. Add a platform check after the build:
Mistakes that cause or hide the error
Assuming Docker handles architecture for you. Docker pulls the manifest that matches your host. If the image has only one variant and it does not match, Docker pulls it anyway and the failure shows up at runtime, not at pull time.
Forgetting --platform on Apple Silicon. This is the most common cause of the error in recent development setups. Set --platform linux/amd64 explicitly when building for x86_64 targets.
No .gitattributes rule for line endings. With autocrlf=true, Git converts LF to CRLF on Windows checkouts. Add *.sh text eol=lf to .gitattributes to stop it.
Using FROM scratch with a shell-form entrypoint. A scratch image has no shell and no filesystem tools. A shell-form ENTRYPOINT fails because /bin/sh does not exist, so always use the exec form there.
Not checking docker buildx ls. If Buildx is not configured, multi-platform builds silently fall back to a single platform. Confirm your builder supports the target platforms.
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 interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.