Docker
exec format error
troubleshooting
container issues
/usr/bin/sh

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:

bash
uname -m
# x86_64, aarch64, armv7l, etc.

Image architecture:

bash
docker inspect --format='{{.Os}}/{{.Architecture}}' your_image:tag
# Example output: linux/arm64

Platforms available in the image manifest:

bash
docker manifest inspect --verbose your_image:tag | grep architecture

If the host reports x86_64 and the image reports arm64, that is the problem.

Shebang in the entrypoint script:

bash
docker run --rm --entrypoint cat your_image:tag /entrypoint.sh | head -1
# Should show: #!/bin/sh or #!/bin/bash

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:

bash
docker pull --platform linux/amd64 node:20-alpine

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:

bash
docker build --platform linux/amd64 -t myapp:latest .

To support several architectures, build with Docker Buildx:

bash
1# Create a builder instance (one-time setup)
2docker buildx create --name multiarch --use
3
4# Build and push multi-arch image
5docker buildx build \
6  --platform linux/amd64,linux/arm64 \
7  -t myregistry/myapp:latest \
8  --push .

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:

bash
1# Register QEMU handlers (one-time, persists until reboot)
2docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
3
4# Now you can run ARM images on x86_64
5docker run --platform linux/arm64 arm64v8/alpine:3.19 uname -m
6# Output: aarch64

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:

dockerfile
1# Dockerfile
2COPY entrypoint.sh /entrypoint.sh
3RUN chmod +x /entrypoint.sh
4ENTRYPOINT ["/entrypoint.sh"]

The script must start with a valid shebang:

bash
1#!/bin/sh
2set -e
3echo "Starting application..."
4exec "$@"

Convert Windows line endings:

bash
# Convert CRLF to LF before building
sed -i 's/\r$//' entrypoint.sh

Or do the conversion in the Dockerfile:

dockerfile
COPY entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r$//' /entrypoint.sh && chmod +x /entrypoint.sh

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:

dockerfile
1# Shell form - invokes /bin/sh -c, can cause issues
2ENTRYPOINT ./myapp
3
4# Exec form - runs the binary directly, preferred
5ENTRYPOINT ["/myapp"]

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

CauseSymptomFix
Architecture mismatch (ARM image on x86)Error on container startPull correct platform or rebuild
Architecture mismatch (x86 image on ARM Mac)Error on container startdocker pull --platform linux/amd64 + Rosetta/QEMU
Missing shebang in entrypoint scriptError on container startAdd #!/bin/sh as first line
Windows CRLF line endings in scriptError on container startConvert to LF with sed or .gitattributes
Shell form ENTRYPOINT with no /bin/shError on container startSwitch to exec form ENTRYPOINT ["/myapp"]
Corrupt or incomplete image layersError on container startdocker 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:

text
Docker Desktop -> Settings -> General -> "Use Rosetta for x86_64/amd64 emulation on Apple Silicon"

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:

bash
docker build --platform linux/amd64 -t myapp:latest .

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:

bash
1# In your CI script, after building
2EXPECTED_ARCH="amd64"
3ACTUAL_ARCH=$(docker inspect --format='{{.Architecture}}' myapp:latest)
4if [ "$ACTUAL_ARCH" != "$EXPECTED_ARCH" ]; then
5  echo "ERROR: Image built for $ACTUAL_ARCH but expected $EXPECTED_ARCH"
6  exit 1
7fi

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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design