Docker Logs Explained (Complete Guide for Beginners & Experts)

Docker logs are one of the most powerful tools for debugging containers. Whether your app crashes, fails to start, or behaves unexpectedly, logs give you the answers.

In this guide, you’ll learn everything about Docker logs, from basic commands to advanced logging drivers.


What Are Docker Logs?

Docker logs capture:

  • Application output (stdout & stderr)
  • Errors and warnings
  • Debug information

Basic Docker Logs Command

docker logs <container_id>

Example

docker run -d nginx
docker logs nginx

Follow Logs in Real-Time

docker logs -f <container_id>

Like tail -f in Linux.


Show Last N Lines

docker logs --tail 50 <container_id>

Show Logs with Timestamps

docker logs -t <container_id>

Combine Options

docker logs -f --tail 100 -t <container_id>

Where Docker Logs Are Stored

Default location:

/var/lib/docker/containers/<container-id>/<container-id>-json.log

Log Rotation (Very Important)

Without rotation, logs can fill disk.

Configure in daemon.json

sudo nano /etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

Restart Docker:

sudo systemctl restart docker

Docker Logging Drivers

1. json-file (default)

  • Stores logs locally

2. syslog

Send logs to system logger.


3. journald

Used in systemd systems.


4. fluentd

For centralized logging.


5. awslogs

Send logs to AWS CloudWatch.


Example: Use Logging Driver

docker run --log-driver=syslog nginx

Debugging with Logs

Common Errors

Container exits immediately

docker logs <container_id>

Application crash

Check error stack traces.


Port binding issues

Look for:

address already in use

Best Practices

1. Always Enable Log Rotation

Prevents disk full errors.


2. Centralize Logs

Use ELK stack:

  • Elasticsearch
  • Logstash
  • Kibana

3. Use Structured Logging

JSON format logs are easier to parse.


4. Monitor Logs

Combine with Grafana dashboards.


Advanced: Docker Compose Logging

services:
  app:
    image: myapp
    logging:
      driver: "json-file"
      options:
        max-size: "10m"

Clear Docker Logs Safely

truncate -s 0 /var/lib/docker/containers/*/*-json.log

Troubleshooting Checklist

  • Container running?
  • Logs accessible?
  • Disk space available?
  • Logging driver correct?

Real-World Example

Problem:

Container keeps restarting.

Solution:

docker logs -f container_name

Identify error → fix config → restart container.


When to Use Managed Logging

Managing logs manually can be complex.

👉 Use managed hosting:

The Ultimate Managed Hosting Platform

Benefits:

  • Centralized logs
  • Automatic rotation
  • Easy monitoring

Conclusion

Docker logs are your first line of debugging.

Key Takeaways:

✔ Use docker logs effectively
✔ Enable log rotation
✔ Centralize logs for production
✔ Monitor continuously

Mastering Docker logs will save you hours of debugging.


👉 Want to skip manual setup? Use:

The Ultimate Managed Hosting Platform

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *