How do you view and follow the logs of a running container?
Use docker logs <container>. Add -f to follow new lines, --tail N to show only the last N lines, and --since to limit by time. It shows whatever the main process wrote to stdout and stderr.
Explain it simply
A container's logs are like the CCTV recording of a shop: you can replay all of it, jump to the last few minutes, or watch live.
Docker captures everything the main process writes to stdout and stderr and stores it using a logging driver (JSON files by default). That is why containerised apps should log to the console, not to a file inside the container. docker logs web prints everything so far. -f keeps the stream open like tail -f. --tail 50 limits output to the last 50 lines, --since 10m to the last ten minutes, and -t adds timestamps.
If docker logs is empty although the app is running, the app is probably writing to a file instead of the console. In Kubernetes the matching command is kubectl logs.
Example
docker logs web
docker logs -f --tail 50 web # live, starting from last 50 lines
docker logs --since 10m -t web # last 10 minutes with timestamps
kubectl logs -f my-pod # Kubernetes equivalentWhat to say in the interview
"docker logs shows the container's stdout and stderr. I use -f to follow, --tail to limit lines and --since for a time window. If it is empty, the app likely logs to a file rather than the console."
Common mistakes
- Logging to a file inside the container, which is lost on removal and invisible to docker logs.