Viewing Container Logs & Debugging (docker logs, inspect, stats)
Use logs, inspect, and stats to diagnose crashes, resource spikes, and misconfig.
In plain words
Read the exit evidence and logs first, then correlate state and live resource counters.
Think of it like…
Logs are the cockpit voice recorder, inspect is the maintenance report, and stats is the live set of gauges.
Where you’ve seen it
In practice, docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream api is the concrete move for this job: use logs, inspect, and stats to diagnose crashes, resource spikes, and misconfig. The release is not accepted until correlate the 10-minute log timeline with container state and a one-shot resource sample confirms the result.
Words you’ll hear
- logs
- The implementation control in
docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream apifor use logs, inspect, and stats to diagnose crashes, resource spikes, and misconfig. - inspect
- The result to observe with
correlate the 10-minute log timeline with container state and a one-shot resource sample, rather than infer from a successful command alone. - stats
- The production boundary exposed when mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window.
Visual diagram
Theory & deep dive
logs control path
The concrete mechanism is docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream api. The observable result is correlate the 10-minute log timeline with container state and a one-shot resource sample; if those disagree, inspect the runtime evidence before changing unrelated settings.
Use logs, inspect, and stats to diagnose crashes, resource spikes, and misconfig. Keep logs, inspect and stats in the same deployment record so the intent and the observed state cannot drift apart.
docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream api
correlate the 10-minute log timeline with container state and a one-shot resource sample, not with command success alone.stats failure boundary
Starting another copy before collecting State.Error, OOMKilled and exit code destroys the clearest signal. The log command can also fail or be incomplete with non-json drivers, so observability should not depend on a single CLI path. The narrow failure to design for is mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window.
Accept this compromise explicitly: CLI inspection is immediate during an incident; a metrics/log backend preserves history and allows correlation across many services. The controlled check-correlate the 10-minute log timeline with container state and a one-shot resource sample-also identifies the point where a different design is safer.
Exercise this failure safely: mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: Starting another copy before collecting State.Error, OOMKilled and exit code destroys the clearest signal. The log command can also fail or be incomplete with non-json drivers, so observability should not depend on a single CLI path.
- Intent
- logs
When to use it & trade-offs
What it is
docker logs reads the stdout/stderr stream captured by the configured logging driver, docker inspect supplies state, environment and error details, and docker stats samples CPU, memory, block I/O and network counters from the runtime. Together they distinguish an app crash from resource pressure or bad configuration.
Why it matters: Starting another copy before collecting State.Error, OOMKilled and exit code destroys the clearest signal. The log command can also fail or be incomplete with non-json drivers, so observability should not depend on a single CLI path.
When to reach for it
- The fit is strongest for this job: use logs, inspect, and stats to diagnose crashes, resource spikes, and misconfig. Pair it with
correlate the 10-minute log timeline with container state and a one-shot resource samplein the runbook. - Production logs changes should make inspect observable through
docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream api, rather than trusting the runtime default. - Keep a rollback example for the case where mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window.
When to avoid or be careful
- Do not accept command success while the system can still fail by mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window.
- Avoid erasing stats protection merely to dodge this cost: “CLI inspection is immediate during an incident; a metrics/log backend preserves history and allows correlation across many services”.
- Do not replace the evidence path-
correlate the 10-minute log timeline with container state and a one-shot resource sample-with a screenshot or a mutable tag.
docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream api: practical choices
Strengths: docker logs reads the stdout/stderr stream captured by the configured logging driver, docker inspect supplies state, environment and error details, and docker stats samples CPU, memory, block I/O and network counters from the runtime. Together they distinguish an app crash from resource pressure or bad configuration.
Watch out: CLI inspection is immediate during an incident; a metrics/log backend preserves history and allows correlation across many services.
Strengths: It can begin with docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream api and fewer surrounding controls.
Watch out: It silently accepts mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window, which makes the result hard to reproduce after promotion.
Strengths: Starting another copy before collecting State.Error, OOMKilled and exit code destroys the clearest signal. The log command can also fail or be incomplete with non-json drivers, so observability should not depend on a single CLI path.
Watch out: The platform adds an ownership and debugging layer; retain correlate the 10-minute log timeline with container state and a one-shot resource sample as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream api
- correlate the 10-minute log timeline with container state and a one-shot resource sample
- Failure signature: mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window
- Trade-off: CLI inspection is immediate during an incident; a metrics/log backend preserves history and allows correlation across many services.
Benchmarks & rules of thumb
- 137
- 128 plus SIGKILL, commonly an OOM kill
- 143
- 128 plus SIGTERM, commonly a graceful-stop timeout
- 10 minutes
- a focused recent log window for triage
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream apias proof and skippingcorrelate the 10-minute log timeline with container state and a one-shot resource sample. - Mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window
- Optimizing logs without accepting that cLI inspection is immediate during an incident; a metrics/log backend preserves history and allows correlation across many services.
Likely interviewer follow-ups
- Why does
docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream apiaddress logs here rather than through application code? - How would you distinguish healthy inspect from the failure “mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window” using
correlate the 10-minute log timeline with container state and a one-shot resource sample? - When would this trade-off-“CLI inspection is immediate during an incident; a metrics/log backend preserves history and allows correlation across many services”-justify a different stats design?
docker logs --since 10m --timestamps api; docker inspect -f {{.State.ExitCode}}:{{.State.OOMKilled}} api; docker stats --no-stream api-and one proof-correlate the 10-minute log timeline with container state and a one-shot resource sample. Then name the boundary: mistaking an exit code 137 for an application bug without checking memory events, or relying on logs after rotation has removed the needed window.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Viewing Container Logs & Debugging (docker logs, inspect, stats) with generative AI interview Q&A and hands-on practice.