Docker has become a standard tool for containerization, enabling developers and DevOps engineers to build, ship, and run applications consistently across environments. However, while working with Docker containers, you may encounter a confusing error message:
docker exec /usr/bin/sh: exec format error
This error often appears when you attempt to access a running container using docker exec. For beginners, it can be frustrating because the container may appear to be running fine, yet you cannot enter it.
In this detailed guide, we will explain what this error means, why it happens, and how to fix it using real-world examples and practical Docker commands. By the end of this article, you will understand how to prevent this issue in future Docker projects.
The exec format error occurs when Docker tries to execute a binary that is incompatible with the system architecture of the container runtime. In simple terms, Docker is trying to run a shell (/usr/bin/sh) that was built for a different CPU architecture.
docker exec -it my_container /usr/bin/sh exec /usr/bin/sh: exec format error
The most common reason for this error is an architecture mismatch. For example:
When Docker tries to execute a binary compiled for AMD64 on an ARM64 system, it fails with an exec format error.
Some Docker images are built using minimal or custom base images that do not include a compatible shell binary.
If the image was partially downloaded or incorrectly built, required binaries may not work as expected.
Some images do not contain /usr/bin/sh. Instead, they may include:
You are using an Apple M1 Mac (ARM64) and pull an image built for AMD64:
docker pull ubuntu
Then you run:
docker run -d --name test ubuntu sleep 1000
When you try to exec:
docker exec -it test /usr/bin/sh
Docker may respond with:
exec format error
docker version
docker inspect test | grep Architecture
If the architecture does not match your system, you need a compatible image.
When pulling or running an image, specify the platform:
docker run --platform linux/amd64 -it ubuntu /bin/bash
This forces Docker to use emulation where supported.
Many official images support multiple architectures. Always prefer them:
docker pull --platform linux/arm64 nginx
docker exec -it my_container /bin/sh
If bash is available:
docker exec -it my_container /bin/bash
docker buildx build --platform linux/arm64 -t myimage:latest .
docker exec [OPTIONS] CONTAINER COMMAND
| Host Architecture | Image Architecture | Result |
|---|---|---|
| AMD64 | AMD64 | Works |
| ARM64 | ARM64 | Works |
| ARM64 | AMD64 | Exec format error |
Understanding this error will save you hours of debugging and help you build more portable, reliable Docker applications across different environments.
Yes, a container can run successfully while docker exec fails due to binary incompatibility.
Yes, it is very common due to ARM64 and AMD64 architecture differences.
No, you can use /bin/sh or /bin/bash depending on the image.
Docker cannot automatically fix architecture mismatches; the correct image or platform must be used.
Emulation works for development and testing but is not recommended for high-performance production workloads.
The Docker exec /usr/bin/sh: exec format error is primarily caused by CPU architecture mismatches or incorrect shell paths. While the error may seem intimidating at first, it is usually straightforward to fix by verifying architectures, using the correct platform, or rebuilding images properly.
Copyrights © 2024 letsupdateskills All rights reserved