error docker

Docker Exit Code 126 - Command Not Executable

Understanding Docker exit code 126 - the container's entrypoint or command was found but could not be executed.

What It Means

Docker exit code 126 indicates that the command specified in the container’s CMD or ENTRYPOINT was found but could not be executed. This typically means the file exists but lacks execute permissions, has an unrecognized format, or has Windows-style line endings corrupting the shebang line.

Common Causes

  • Entrypoint script missing execute permission (chmod +x)
  • Windows line endings (CRLF) in a shell script breaking the shebang
  • Binary compiled for a different architecture (e.g., amd64 binary on ARM)
  • Script without a proper shebang line (#!/bin/bash)
  • File permissions lost during Docker build (COPY without preserving permissions)
  • Trying to execute a directory instead of a file

How to Fix

Fix execute permissions in Dockerfile

# Add execute permission to scripts
COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
ENTRYPOINT ["/app/entrypoint.sh"]

# Or set permissions during COPY (BuildKit required)
COPY --chmod=755 entrypoint.sh /app/entrypoint.sh

Fix Windows line endings

# Convert CRLF to LF before building
# On Linux/macOS:
sed -i 's/\r$//' entrypoint.sh

# Or use dos2unix
dos2unix entrypoint.sh
# Fix in Dockerfile
RUN apt-get update && apt-get install -y dos2unix
COPY entrypoint.sh /app/entrypoint.sh
RUN dos2unix /app/entrypoint.sh && chmod +x /app/entrypoint.sh

Add a .gitattributes file to prevent line ending issues:

*.sh text eol=lf
entrypoint.sh text eol=lf

Add proper shebang line

#!/bin/bash
# or
#!/bin/sh
# This MUST be the very first line of the script

set -e
echo "Starting application..."
exec "$@"

Fix architecture mismatch

# Check your image architecture
docker inspect --format='{{.Architecture}}' my-image

# Build for the correct platform
docker build --platform linux/amd64 -t my-image .

# Or use multi-platform build
docker buildx build --platform linux/amd64,linux/arm64 -t my-image .

Debug the issue

# Override entrypoint to inspect the file
docker run -it --entrypoint /bin/sh my-image

# Check the file
ls -la /app/entrypoint.sh
file /app/entrypoint.sh
head -1 /app/entrypoint.sh | cat -v  # Shows ^M for CRLF