How to Follow Docker Container Logs Easily
Use the
docker logs -f <container_name_or_id> command to follow container logs in real-time. The -f flag streams new log entries as they appear, letting you monitor your container output live.Syntax
The basic syntax to follow Docker container logs is:
docker logs: Shows logs of a container.-for--follow: Streams logs live, like watching a live feed.<container_name_or_id>: The name or ID of the container whose logs you want to see.
bash
docker logs -f <container_name_or_id>
Example
This example shows how to follow logs of a running container named myapp. It streams new log lines as they appear.
bash
docker logs -f myapp
Output
[2024-06-01T12:00:01Z] Server started on port 8080
[2024-06-01T12:00:05Z] Received request /home
[2024-06-01T12:00:10Z] Sent response 200 OK
Common Pitfalls
Some common mistakes when following container logs:
- Not using the
-fflag, which shows only existing logs and exits immediately. - Using the wrong container name or ID, resulting in an error.
- Trying to follow logs of a stopped container, which shows logs but no live updates.
Always check your container is running with docker ps before following logs.
bash
docker logs myapp # This shows logs once and exits docker logs -f wrongname # Error: No such container # Correct way: docker logs -f myapp
Quick Reference
| Command | Description |
|---|---|
| docker logs | Show all logs of the container once |
| docker logs -f | Follow logs live as new entries appear |
| docker ps | List running containers to get names or IDs |
| docker logs --tail 10 | Show only last 10 log lines |
Key Takeaways
Use
docker logs -f <container> to follow logs live.Check container name or ID with
docker ps before following logs.Without
-f, logs show once and then stop.Following logs of stopped containers shows no live updates.
Use
--tail to limit log lines shown.