How do you view logs from a running Docker container?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
To view logs from a running Docker container, you use the docker logs command followed by the container name or ID.
So if my container is called order-api, I'd run docker logs order-api. That shows all the output — everything your Spring Boot app wrote to stdout and stderr. But in practice, I almost always use the -f flag to follow logs in real time, like docker logs -f order-api.
That's crucial during development because you want to see what happens when you call an API endpoint or trigger some feature. Without -f, you're constantly rerunning the command and missing events between executions. Now, if the container has been running for a while, docker logs dumps thousands of lines and you can't find anything useful.
That's where --tail comes in. I use docker logs --tail 50 order-api to see just the last 50 lines, which skips all the Spring Boot startup noise and shows recent activity. Another really helpful flag is --since for filtering by time.
Like docker logs --since 10m shows logs from the last 10 minutes. I combine these when debugging: docker logs --since 5m --tail 100 -f order-api. That starts with the last 100 lines from the last 5 minutes, then follows new output.
It's perfect for investigating an error that just happened without scrolling through historical logs. One thing freshers forget is that once a container stops, logs stop too. If you're using -f and nothing appears, check docker ps -a to see if the container crashed.
Then run docker logs without -f to see what happened before it died.
How do you view logs from a running Docker container
Direct Answer
Use docker logs followed by the container ID or name to see all output from a container. For example, docker logs order-api shows stdout and stderr. Add -f to follow logs in real time like tail -f, --tail 100 to see only the last 100 lines, and --since 10m to view logs from the last 10 minutes. Combine flags for targeted debugging.
Code Example
// View all logs from container
docker logs order-api
// Follow logs in real time (essential during development)
docker logs -f order-api
// See only last 50 lines (avoid scrolling through startup messages)
docker logs --tail 50 order-api
// View logs from last 10 minutes
docker logs --since 10m order-api
// Combine flags for targeted debugging
docker logs --since 5m --tail 100 -f order-api
// Filter logs with grep for specific errors
docker logs order-api | grep ERROR
// View logs with timestamps
docker logs -t order-api
// Save logs to file for analysis
docker logs order-api > order-api-logs.txt
Common Mistakes & Pitfalls
- Not using -f for real-time debugging: Freshers run docker logs repeatedly in a loop, hitting up-arrow and Enter, missing events between executions. Using -f keeps the stream open so you see every log line as it happens — critical when testing API calls or watching for specific errors.
- Overwhelming the terminal with historical logs: Running docker logs without --tail on a container that's been running for hours dumps thousands of lines, making it impossible to find recent errors. Always use --tail or --since to limit output.
- Forgetting containers stop writing logs after exit: If the container crashed, docker logs shows output up to the crash, but no new lines appear even with -f. Check docker ps -a to see if the container exited, then read logs to find the cause.
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.