Docker Logs Explained: How to View and Manage Container Logs
When working with Docker, one of the most important tasks during development or troubleshooting is viewing logs.
Whether you're debugging an error or just monitoring your application, the docker logs command is your best friend.
In this post, you'll learn:
- 🧠 What Docker logs are
- 🛠️ How to use the
docker logscommand - 📚 Common options and flags
- 🧪 Real-world examples
- ✅ Best practices
Let’s get logging! 🚀
📦 What Are Docker Logs?
Docker logs are output streams (stdout and stderr) from containers. Anything your application prints (like console.log, print(), or error messages) will show up in the logs.
By default, Docker captures these logs and stores them using a logging driver (like json-file).
📄 Basic Syntax
docker logs [OPTIONS] CONTAINER
Replace CONTAINER with the container name or ID.
Example:
docker logs my-container
🛠️ Commonly Used docker logs Options
Here are the most useful flags you can use with the docker logs command:
| Option | Description |
|---|---|
-f or --follow |
Stream logs in real-time (like tail -f) |
--since |
Show logs since a given time (e.g., --since="10m") |
--tail |
Show only the last N lines (e.g., --tail 100) |
-t or --timestamps |
Include timestamps with each log line |
--until |
Show logs up until a certain time |
🧪 Practical Examples
▶️ 1. View logs from a container
docker logs my-container
📺 2. Follow logs in real time
docker logs -f my-container
This is useful when you're running an app and want to watch the output live (like a web server).
⏱️ 3. View logs from the last 15 minutes
docker logs --since 15m my-container
Or a specific time:
docker logs --since="2024-04-25T10:00:00" my-container
📜 4. View only the last 100 lines
docker logs --tail 100 my-container
🕒 5. Add timestamps to each log entry
docker logs -t my-container
🧵 6. Combine flags
docker logs -f --since 10m --tail 50 -t my-container
This shows the last 50 lines of logs from the last 10 minutes, with timestamps, and keeps streaming new lines as they come in.
🔍 How to Find Your Container Name or ID
Use the following command to list running containers:
docker ps
This will show something like:
CONTAINER ID IMAGE COMMAND STATUS NAMES
123abc456def myapp:latest "npm start" Up 5 mins my-container
Now you can use docker logs my-container or docker logs 123abc456def.
⚙️ Where Are Docker Logs Stored?
By default, logs are stored in:
/var/lib/docker/containers/<container-id>/<container-id>-json.log
📝 Tip: You usually don't need to access this file directly unless you’re doing advanced debugging.
📂 Other Logging Drivers
Docker supports multiple logging drivers, like:
json-file(default)syslogjournaldawslogsgelffluentdlogstash
To check which logging driver is being used:
docker inspect my-container --format='{{.HostConfig.LogConfig.Type}}'
✅ Best Practices for Docker Logging
- Use
-fwhile debugging to watch logs in real-time. - Limit log size using log rotation to avoid disk issues.
- Centralize logs for production using tools like ELK stack, Fluentd, or AWS CloudWatch.
- Use timestamps when analyzing issues from the past.
- Never log sensitive data, like passwords or API keys.
🧹 Bonus: Cleaning Up Huge Logs
If logs are getting too large, you can use log rotation in your docker-compose.yml or Docker run command:
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
This keeps 3 files of 10 MB each — automatically rotating when they’re full.
📚 Summary
Here’s a quick recap:
| Task | Command |
|---|---|
| View logs | docker logs container |
| Stream logs | docker logs -f container |
| Last N lines | docker logs --tail N container |
| Logs since | docker logs --since 10m container |
| Logs with time | docker logs -t container |
🛠️ Final Thoughts
Understanding Docker logs is essential for debugging and monitoring your apps. Whether you're building, testing, or running in production, mastering the docker logs command gives you better visibility into your containers.
Now go ahead and try it out in your terminal — and may your logs be ever helpful! 🐳📘
Projects related to this guide
Use the concepts from this article in practical source-code projects.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure...
Python Flask Blog Project with Admin Panel
Are you ready to launch a powerful, modern, and fully customizable blog? Look no further! Introducing the Flask-...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes pr...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.