Docker Course
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 / OptionDescription
-f / --followFollow log output dynamically in real-time.
-n / --tailNumber of lines to show from the end of the logs.
-t / --timestampsShow 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?

Flash Cards

Question

What is the -f flag?

Click to reveal answer
Answer

Follow. Instead of just printing the past logs and quitting, it keeps the terminal open and streams new logs to your screen live, in real-time.

Question

Will it show internal files?

Click to reveal answer
Answer

No. `docker logs` ONLY captures text sent to the terminal (`stdout` and `stderr`). This is why best practice dictates that containerized apps should never write logs to internal `.txt` files.