A Docker container exits immediately with `exec /entrypoint.sh: exec format error`. The script runs fine with `sh /entrypoint.sh`.
IntermediateTroubleshootingTroubleshooting: syntax, parsing and interpreter errors
Answer
Likely causes → The kernel cannot execute the file directly: it has no shebang line (or the shebang is not on the very first line, e.g. after a blank line or a UTF-8 BOM), or it has CRLF endings. Docker's exec-form ENTRYPOINT calls execve(), which needs a valid #! line.
Diagnose →
head -c 20 entrypoint.sh | od -c # must start with "# ! /"; a BOM shows as 357 273 277
file entrypoint.sh
Fix → Make #!/bin/sh (or #!/usr/bin/env bash) the first bytes of the file, strip any BOM and CRLF, and chmod +x.
Prevent → .gitattributes for LF, ShellCheck in CI (it flags a missing shebang with SC2148), and a container smoke test in the pipeline.
More on Troubleshooting: syntax, parsing and interpreter errors
`./deploy.sh` fails with `bash: ./deploy.sh: /bin/bash^M: bad interpreter: No such file or directory` (or, on Bash 5.2, `cannot execute: required file not found`).Beginner`bash: ./backup.sh: Permission denied` right after cloning the repo.Beginner`deploy.sh: line 14: syntax error near unexpected token `fi'`Beginner`install.sh: line 87: syntax error: unexpected end of file`Beginner`warning: here-document at line 12 delimited by end-of-file (wanted `EOF')` and the rest of the script never runs.Beginner`bash: [: missing `]'`Beginner