Docker
/Beginner
docker logs
Definition
The CLI command used to view the standard output (stdout) and standard error (stderr) streams generated by the processes running inside a container.
Explain Like I'm New
Reading the text output of the app. Since containers usually run invisibly in the background, this is the primary way you read the errors to figure out why an app crashed.
Real World Example
A Node.js server container crashes silently. The developer types `docker logs my-api`. The terminal prints out the stack trace showing 'Error: Database connection refused', allowing the developer to fix the issue.
Common Use Cases
- •Debugging crashes
- •Monitoring live activity
Crucial Command Flags
| Flag / Option | Description |
|---|---|
| -f / --follow | Follow log output dynamically in real-time. |
| -n / --tail | Number of lines to show from the end of the logs. |
| -t / --timestamps | Show accurate timestamps for every log line. |
Interview Questions
basic
- What does the `-f` flag do in `docker logs -f my-container`?
intermediate
- If a container writes its logs to a text file deep inside its filesystem instead of to `stdout`, will `docker logs` show them?