Chapter 14: Logging and Light Observability
14.2 stdout/stderr and docker logs
Why: the problem
Suppose Zomato runs its menu service in Docker containers. A container prints its logs on the screen, to stdout and stderr. Nobody is watching that screen. So Docker catches every line and saves it for you. The command docker logs shows those saved lines. This is the first command every DevOps engineer types when a container misbehaves. But there are traps. If the app writes logs into a file inside the container, docker logs shows nothing. If nobody limits the size, the saved log file can fill the whole disk and crash the server. In this lesson we learn the right way: the app writes to stdout and stderr, Docker collects, and we set size limits.
What we will build
You will run a container that prints a line every second, read it with docker logs and its flags, run a Python app and a Spring Boot app in Docker, and set a log size limit with Docker Compose so the disk never fills up.
What: every term in plain English
- stdout — File number 1. Normal output of a program.
- stderr — File number 2. Error output of a program.
- 2>&1 — Shell trick: send stderr to the same place as stdout.
- docker logs — Shows what a container wrote to stdout and stderr.
- -f (follow) — Keep the command open and show new lines live, like tail -f.
- --since / --tail — Show only lines after a time, or only the last N lines.
- Logging driver — The part of Docker that decides where container logs go. Default is json-file.
- max-size / max-file — Options of json-file that limit size and number of log files.
How: step by step
-
Start a ticker container
docker run -d --name ticker busybox sh -c "i=0; while true; do i=$((i+1)); echo tick $i; sleep 1; done"What it does: It prints one line each second to stdout.
Output
8d2c41e0f9a7... -
Read all logs
docker logs tickerWhat it does: Shows every line so far.
Output
tick 1 tick 2 tick 3 -
Follow live
docker logs -f --tail 2 tickerWhat it does: Shows the last 2 lines and then waits for new ones. Ctrl+C to stop.
Output
tick 9 tick 10 tick 11 -
Add timestamps
docker logs -t --tail 1 tickerWhat it does: Docker adds its own time to each line.
Output
2026-10-25T03:40:12.118Z tick 12 -
Lines from the last minute
docker logs --since 1m ticker | wc -lWhat it does: Count lines from the last 60 seconds.
Output
60 -
See where Docker stores them
docker inspect -f "{{.LogPath}}" tickerWhat it does: The real json file on the host disk.
Output
/var/lib/docker/containers/8d2c.../8d2c...-json.log
Example 1: Python app in Docker that logs to stdout
Stack. Python 3.12, Docker
Goal. Python app in Docker that logs to stdout.
app.py
import logging, sys, time
logging.basicConfig(stream=sys.stdout, level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("menu")
for i in range(1, 4):
log.info("menu loaded restaurant=%d", i)
time.sleep(1)
log.error("price service not reachable")
Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
ENV PYTHONUNBUFFERED=1
CMD ["python", "app.py"]
What each line does
stream=sys.stdout— Send log lines to stdout so Docker can catch them.ENV PYTHONUNBUFFERED=1— Print each line at once. Without it, lines can be delayed.CMD ["python", "app.py"]— The app is process 1 in the container.
Green output
$ docker build -t ravi/menu:1.0 . && docker run --name menu ravi/menu:1.0
$ docker logs menu
2026-10-25 03:45:01,001 INFO menu loaded restaurant=1
2026-10-25 03:45:02,003 INFO menu loaded restaurant=2
2026-10-25 03:45:03,005 INFO menu loaded restaurant=3
2026-10-25 03:45:04,006 ERROR price service not reachable
Expected result. docker logs shows every line, even after the container stopped.
Example 2: Spring Boot in Docker: read only the errors
Stack. Java 17, Spring Boot 3, Docker
Goal. Spring Boot in Docker: read only the errors.
Commands
docker run -d --name orders -p 8080:8080 ravi/orders-springboot:1.0
docker logs orders 2>&1 | grep -E "ERROR|WARN" | tail -n 5
docker logs --since 2026-10-25T03:00:00 --until 2026-10-25T03:10:00 orders | grep ERROR
What each line does
2>&1— Join stderr into stdout so grep sees both.grep -E "ERROR|WARN"— Keep only error and warning lines.--since ... --until ...— Only lines in that 10 minute window, like an incident window.
Green output
2026-10-25T03:04:10.551Z WARN 1 --- [main] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection
2026-10-25T03:04:12.903Z ERROR 1 --- [main] o.s.boot.SpringApplication : Application run failed
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection
Expected result. In seconds you know the app failed because it cannot reach the database.
Example 3: Limit log size with Docker Compose
Stack. Docker Compose v2, nginx
Goal. Limit log size with Docker Compose.
compose.yaml
services:
web:
image: nginx:1.27
ports:
- "8081:80"
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
/etc/docker/daemon.json (for all containers)
{
"log-driver": "json-file",
"log-opts": { "max-size": "10m", "max-file": "3" }
}
What each line does
logging: driver: json-file— Use the default file driver.max-size: "10m"— One log file is at most 10 MB.max-file: "3"— Keep only 3 files. Total 30 MB per container.daemon.json— Default for every new container. Restart Docker after editing.
Green output
$ docker compose up -d
[+] Running 1/1
Container loglab-web-1 Started
$ docker inspect -f "{{.HostConfig.LogConfig}}" loglab-web-1
{json-file map[max-file:3 max-size:10m]}
Expected result. The nginx container can never use more than 30 MB of disk for logs.
Verify: did it work?
-
Check log config
docker inspect -f "{{.HostConfig.LogConfig.Type}}" tickerWhat it does: Shows the logging driver.
Output
json-file -
Check disk used by logs
sudo du -sh /var/lib/docker/containers/*/*-json.logWhat it does: Shows size of each container log file.
Output
12K /var/lib/docker/containers/8d2c.../8d2c...-json.log
When it is red 1
docker logs shows nothing, but the app is running
Cause. The app writes to a file inside the container, not to stdout.
Fix, step by step
- Change the app to log to stdout.
- Or link the file: RUN ln -sf /dev/stdout /var/log/app.log in the Dockerfile.
- Rebuild and run again.
When it is red 2
Error response from daemon: configured logging driver does not support reading
Cause. The container uses a driver like awslogs or syslog that docker logs cannot read (on older Docker).
Fix, step by step
- Read logs where the driver sends them, like CloudWatch.
- Or use Docker 20.10+ which keeps a local copy (dual logging).
When it is red 3
no space left on device
Cause. Container log files grew with no limit.
Fix, step by step
- Find big ones: sudo du -sh /var/lib/docker/containers//-json.log | sort -h | tail
- Add max-size and max-file in daemon.json.
- Restart Docker and recreate the containers.
Cleanup
-
Remove lab containers
docker rm -f ticker menu orders ; docker compose downWhat it does: Deletes the lab containers and their logs.
Output
ticker menu orders
Ravindra Bagale's Tip
Do not write logs into a file inside the container — write to stdout. And do not forget max-size, or the disk fills up.
Container मध्ये log file मध्ये नको, stdout वर लिहा. आणि max-size विसरू नका, नाहीतर disk full होईल.
Container में log file में मत लिखो — stdout पर लिखो. और max-size मत भूलो, नहीं तो disk full हो जाएगी.
Practice tasks
Easy Use --tail and -t together
Why: Fast look at the latest lines with time.
What to do: Show the last 5 lines with timestamps.
- docker logs -t --tail 5 ticker
Check: Five lines with Docker time.
Medium Set global limits
Why: One setting protects every container.
What to do: Add log-opts to daemon.json and restart Docker.
- sudo systemctl restart docker
- Run a new container and inspect LogConfig.
Check: New containers show max-size 10m.
Hard Fix a file-logging image
Why: Old images often log to files.
What to do: Make an nginx-like image send logs to stdout with symlinks.
- RUN ln -sf /dev/stdout /var/log/app/access.log
Check: docker logs now shows the access lines.
Easy Say docker logs in two lines
Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.
What to do: Write two short lines about docker logs for a fresher who knows only Linux, Git and Docker basics.
- Write the problem it solves.
- Write the one command or config line you use for it.
- Read it aloud to a friend.
Check: A fresher understands it without asking a question.
Easy Keep a throwaway log lab
Why: Practice needs a lab you can break. If it gets messy, you delete the containers and start again.
What to do: Run a small app in Docker that prints logs every second.
- docker run -d --name ticker busybox sh -c "while true; do date; sleep 1; done"
- docker logs -f ticker
- Press Ctrl+C, then docker rm -f ticker.
Check: You see one new time line every second.
Medium Do it on a second stack
Why: Logging ideas must work for Python, Node.js and Java. Only the library changes.
What to do: Repeat this lesson with a different language than the one you used first.
- If you used Python, use Node.js or Spring Boot now.
- Keep the same field names: time, level, msg, request_id.
- Compare the two outputs side by side.
Check: Both apps print the same fields in the same shape.
Medium Break it on purpose
Why: You must know the red error by face. In a real outage there is no time to read slowly.
What to do: Cause one error from the "When it is red" part of this lesson and fix it.
- Follow the error section.
- Copy the exact message into your notes.
- Write the fix in one line.
Check: You fixed it using only the steps, without searching the web.
Medium Write a one-page runbook
Why: At 2 a.m. nobody remembers log commands. A short page saves the on-call person.
What to do: Write a one-page runbook for this lesson.
- Title, when to use, exact commands, how to check, how to undo.
Check: A teammate can follow it alone.
Hard Explain it like an interview
Why: Interviewers want real stories, not definitions.
What to do: Record a 60 second answer about this lesson.
- Say the problem, the command you ran, the log line you found and the fix.
Check: Your answer has a real command and a real log line.
Hard Draw the flow top to bottom
Why: A drawing shows where a log line is born and where it ends up.
What to do: Draw this lesson as boxes from top to bottom.
- Start at the user click, go through the app, the log driver or agent, and end at the place you search.
Check: Each box matches a real command or file in this lesson.
Homework: try at home
Try at home
At home, run two containers, one Python and one Node.js, both logging to stdout. Use docker logs --since 2m on each and count ERROR lines.
Recap
- Apps in containers must log to stdout and stderr.
- docker logs reads what Docker caught.
- -f, --tail, --since and -t are your daily flags.
- json-file is the default driver.
- Always set max-size and max-file.
FAQ
Where are docker logs stored?
In /var/lib/docker/containers/ID/ID-json.log on the host.
Do logs go away when I stop a container?
No. They go away when you remove it.
Can I see logs of all Compose services?
Yes: docker compose logs -f.
Is kubectl logs the same?
Yes, same idea for Kubernetes pods.
Why do I see logs twice?
The app may print to both stdout and a file that is linked to stdout. Keep one.
Interview questions
-
How do you read logs of a container?
docker logs with -f, --tail and --since, or docker compose logs for many services.
-
Why should containers log to stdout?
Docker and Kubernetes collect stdout automatically; files inside containers are lost and hidden.
-
What is a logging driver?
A Docker plugin that decides where logs go: json-file, local, journald, awslogs, fluentd and more.
-
How do you stop logs filling the disk?
Set max-size and max-file in daemon.json or per service in Compose.
-
Difference between stdout and stderr?
Both are output streams; stderr is for errors. Docker keeps both.
Quick quiz
Q1. Which command shows container logs?
docker logs NAME.
Q2. What does -f do?
Follows new lines live.
Q3. Why PYTHONUNBUFFERED=1?
So Python prints each line at once.
Q4. What is the default logging driver?
json-file.
Q5. Which options limit log size?
max-size and max-file.
Did you understand? Let us try it once more.
Revision card
Interview card
Comparison card