Exit Code 126
The command specified inside the container was found, but it could not be executed due to permission limitations or file format issues.
Usually happens because:
- ☑ Script lacks user execution flags (+x)
- ☑ Target command is a directory, not a file
- ☑ Mount volume is configured with noexec flag
🔍 Quick Checklist:
What is Exit Code 126?
Exit code 126 is a standard POSIX error returned when a command is located inside the container's file system, but the execution attempt is blocked. In Docker, this most commonly occurs when: 1) the script or binary file lacks execution permissions (missing 'chmod +x' flag), 2) you attempted to execute a directory as a command, or 3) the container was mounted using the 'noexec' mount option.
Common Causes
- Missing execution permissions: The shell script or binary inside the container does not have user execution permissions.
- Attempting to execute a directory: Supplying a path that resolves to a folder instead of a runnable file.
- Volume mounted with noexec: The container storage volume or mount path prevents binary execution.
| Cause | Frequency |
|---|---|
| Missing script execution permissions (+x) | ⭐⭐⭐⭐⭐ |
| Executing directory path by mistake | ⭐⭐⭐⭐ |
| noexec flag inside host mounts | ⭐⭐ |
Common Mistakes
- Mapping host folders into containers and attempting to execute scripts inside them directly, without verifying if host permissions match container user privileges.
- Accidentally typing a directory name (like `/usr/local/bin`) inside ENTRYPOINT or CMD instead of the exact script file path.
How to Fix
Docker Operations & Verification
Always add execute privileges to entrypoint scripts copied from the host.
FROM alpine:3.18
COPY entrypoint.sh /usr/local/bin/
# Fix: ensure script is marked executable
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]Platform Specific Fixes
Linux file system mount checks for noexec restrictions.
# Check if the mount directory has noexec flags enabled
mount | grep "/var/lib/docker"Best Practices
- Integrate `git config core.filemode true` inside developer workflow documentation.
- Always use the `chmod +x` instruction inside Dockerfiles for all custom script additions.
Frequently Asked Questions (FAQ)
Q: What is exit code 126?
Exit code 126 means 'Command invoked cannot execute'. The file exists inside the container, but it is not runnable.
Q: How do I fix permission denied inside exit code 126?
Ensure the file is marked executable. Inside your Dockerfile, add a line: 'RUN chmod +x /path/to/script.sh'.
Q: Can Windows file checkouts trigger this?
Yes. Git running on Windows does not natively track the Unix execute permission bit. When the code is cloned on Windows and copied into a container, scripts lose their executable state, triggering exit code 126.
Q: How do I check local file permissions inside the container?
Run 'docker run -it --entrypoint /bin/sh <image>' and run 'ls -la' to inspect the execution bit flags of the target command file.