🐳 Docker: Fix a Container Stuck in a Tight Restart Loop With Nothing Ever Printed in docker logs Because the Entrypoint Script Crashes Before Its Output Buffer Gets Flushed to Stdout
Docker captures whatever a container’s PID 1 process writes to stdout and stderr, but a process that buffers its output — many scripting language runtimes default to fully buffering stdout when it isn’t connected to an interactive terminal — only hands that text to the OS, and therefore to Docker’s log driver, when the buffer is explicitly flushed or the process exits cleanly. An entrypoint that crashes hard before that flush happens loses every line it had written so far, so docker logs shows a completely empty output for a container that’s visibly crash-looping.
🔎 The Problem
$ docker logs flaky-container
(empty output)
$ docker inspect flaky-container --format '{{.State.ExitCode}}'
139 <- SIGSEGV, process terminated abnormally before its stdout buffer flushed
$ docker run -it --entrypoint sh flaky-container # ran interactively instead
Loading config...
Connecting to database...
Segmentation fault (core dumped) <- the output was always there, just never flushed to the log driver
✅ Fix: Stop Buffering, and Reproduce the Crash Interactively
- Run the entrypoint with its runtime’s output buffering disabled for non-interactive stdout (for example
PYTHONUNBUFFERED=1for Python, or explicitly flushing after each write) so lines are handed to Docker’s log driver as they’re written instead of waiting in a buffer a crash can discard. - If the entrypoint is a shell script wrapping another process,
execthe real process as the last line instead of running it as a backgrounded or piped child, so it becomes PID 1 directly and its own output and exit code aren’t lost behind a wrapper that can swallow both. - Reproduce the crash by running the image interactively (
docker run -it --entrypoint sh <image>, then launching the real command by hand) so the crash’s actual output lands on your terminal in real time, independent of whatever buffering affects the detached container’s log driver.
🚨 Why This Is Dangerous
An empty docker logs output on a crash-looping container is easy to misread as “the container never even started,” sending you to check image builds, registry pulls or scheduler configuration instead of the actual crash that logs would have shown if it had ever been flushed.
Docker can only log what the process actually hands to stdout — a crash that happens before a flush doesn’t produce an empty log because nothing went wrong, it produces one because the evidence never left the buffer.

