AWS

Docker exec /usr/bin/sh: exec format error 

Introduction

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.

What Is the Docker exec /usr/bin/sh: exec format error?

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.

Common Symptoms

  • The container starts successfully
  • docker ps shows the container as running
  • Running docker exec fails immediately
docker exec -it my_container /usr/bin/sh exec /usr/bin/sh: exec format error

Why Does Docker exec Format Error Occur?

1. CPU Architecture Mismatch

The most common reason for this error is an architecture mismatch. For example:

  • Your host machine uses ARM64 (Apple M1/M2, Raspberry Pi)
  • The Docker image is built for AMD64

When Docker tries to execute a binary compiled for AMD64 on an ARM64 system, it fails with an exec format error.

2. Incorrect Base Image

Some Docker images are built using minimal or custom base images that do not include a compatible shell binary.

3. Corrupted or Incomplete Image

If the image was partially downloaded or incorrectly built, required binaries may not work as expected.

4. Using the Wrong Shell Path

Some images do not contain /usr/bin/sh. Instead, they may include:

  • /bin/sh
  • /busybox/sh

Real-World Scenario Example

Scenario: Apple Silicon (M1/M2) Mac

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

How to Fix Docker exec /usr/bin/sh: exec format error

Solution 1: Check Host and Image Architecture

docker version
docker inspect test | grep Architecture

If the architecture does not match your system, you need a compatible image.

Solution 2: Specify Platform Explicitly

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.

Solution 3: Use Multi-Architecture Images

Many official images support multiple architectures. Always prefer them:

docker pull --platform linux/arm64 nginx

Solution 4: Use Correct Shell Path

docker exec -it my_container /bin/sh

If bash is available:

docker exec -it my_container /bin/bash

Solution 5: Rebuild the Image for the Correct Architecture

docker buildx build --platform linux/arm64 -t myimage:latest .

Understanding docker exec Command

Basic Syntax

docker exec [OPTIONS] CONTAINER COMMAND

Common Use Cases

  • Debugging running containers
  • Inspecting logs or files
  • Running one-off commands

Comparison Table: Architecture Compatibility

Host Architecture Image Architecture Result
AMD64 AMD64 Works
ARM64 ARM64 Works
ARM64 AMD64 Exec format error

Best Practices to Avoid Exec Format Error

  • Always verify image architecture before deployment
  • Use official multi-arch Docker images
  • Specify platform explicitly in CI/CD pipelines
  • Test containers locally before production use

Understanding this error will save you hours of debugging and help you build more portable, reliable Docker applications across different environments.

Frequently Asked Questions (FAQs)

FAQ 1: Can a container run even if docker exec fails?

Yes, a container can run successfully while docker exec fails due to binary incompatibility.

FAQ 2: Is this error common on Apple M1 or M2 Macs?

Yes, it is very common due to ARM64 and AMD64 architecture differences.

FAQ 3: Does docker exec always require /usr/bin/sh?

No, you can use /bin/sh or /bin/bash depending on the image.

FAQ 4: Can Docker automatically fix exec format errors?

Docker cannot automatically fix architecture mismatches; the correct image or platform must be used.

FAQ 5: Is emulation safe for production?

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.

line

Copyrights © 2024 letsupdateskills All rights reserved