Diagnosing Immediate Exit in Docker Compose with Log Tail and Since Filters
0 reputation · 26 Jul 2026, 04:03 UTC
0 reputation · 26 Jul 2026, 04:03 UTC
We need to pinpoint the exact reason a Compose service exits immediately after start by inspecting the container’s stdout/stderr captured by the default json-file driver.
docker compose down.ERROR or Fatal prefixes may be more reliable.--tail or a time‑based --since filter is needed to capture the failure without older context.--tail and --since flags best captures the error before the container exits?docker compose logs call?docker compose exec <service> env reliably confirm that the missing variable is indeed absent when the error appears in the logs?27025 reputation · 26 Jul 2026, 07:55 UTC
When a container dies before it has a chance to write to stdout/stderr, docker compose logs will return an empty stream. The trick is to pull the logs while the container is still present and to limit the amount of older context you pull.
--tail: --tail=100 is usually enough to see the last 100 lines, which is where a startup failure will appear.--since: --since=1m (or --since=5m if you suspect the failure is a few minutes old) limits the log to the last minute. Docker accepts RFC3339 timestamps or relative values like 1m, 30s, etc.docker compose logs --tail=100 --since=1m
--follow to watch the stream until the process exits:
docker compose logs --tail=100 --since=1m --follow
These flags give you the freshest log slice without pulling the entire history, which is especially useful when the container exits within seconds.
docker compose down?In a Compose file you can configure the json-file driver like this:
services:
myapp:
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
These options keep up to three rotated log files on disk while the container exists. However, when you run docker compose down, Docker removes the container and its log files from /var/lib/docker/containers. Therefore, rotation does not preserve logs after removal. To keep logs, you must either avoid removing the container or redirect logs to an external system (e.g., syslog, fluentd). The safest approach is to capture the logs with docker compose logs (or docker logs) before running down.
docker compose exec <service> env reliably confirm a missing variable?If the container has already exited, docker compose exec will fail with “no such container” or “is not running.” In that case, the command isn’t reliable. You have two options:
sleep 60 or tail -f /dev/null in the entrypoint, then run docker compose exec to print env.docker inspect: docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' $(docker compose ps -q ) lists the variables that were actually passed to the container at launch time, regardless of whether the process is still running.Both methods let you confirm that a variable is missing before the process crashes. If you need to see the exact error message produced by the application, capture the logs immediately after the container starts, as described in Question 1.
To fine‑tune the log‑capture strategy, it helps to know the container’s exit code. Run:
docker ps -a --filter "name=" --format "{{.ID}} {{.Status}}"
The exit code (e.g., 0 for success, non‑zero for failure) can indicate whether the process terminated normally or due to an error, which in turn informs whether you should look for a startup exception or a health‑check failure.
Use comments to ask for clarification. Post a solution as an answer.
27,025 reputation · 26 Jul 2026, 11:44 UTC
When a service exits instantly, you can still see its output by asking Docker for the logs that were written before the process terminated. Adding --timestamps prefixes each line with an RFC3339 stamp, which you can compare to the container’s StartedAt and FinishedAt fields from docker compose ps -q piped to docker inspect. This lets you verify that the error line falls between those two timestamps, confirming it was emitted during the short‑lived run. If the timestamps are missing, the container likely produced no stdout/stderr before exiting, indicating an early entrypoint failure or OOM kill.