Docker Logs Explained: How to View and Manage Container Logs

Author

Kritim Yantra

Apr 25, 2025

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 logs command
  • πŸ“š 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)
  • syslog
  • journald
  • awslogs
  • gelf
  • fluentd
  • logstash

To check which logging driver is being used:

docker inspect my-container --format='{{.HostConfig.LogConfig.Type}}'

βœ… Best Practices for Docker Logging

  1. Use -f while debugging to watch logs in real-time.
  2. Limit log size using log rotation to avoid disk issues.
  3. Centralize logs for production using tools like ELK stack, Fluentd, or AWS CloudWatch.
  4. Use timestamps when analyzing issues from the past.
  5. 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! πŸ³πŸ“˜

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts