Docker  

How to Set Up Logging and Monitoring in Docker Containers

Introduction

When you run applications inside Docker containers, everything looks simple at first. Your app runs, containers start and stop quickly, and deployments become easy. But in real-world production environments, one big challenge appears: how do you track what is happening inside your containers?

For example:

  • What if your application crashes?

  • What if users report slow performance?

  • How do you check logs across multiple containers?

This is where logging and monitoring in Docker containers becomes very important.

Logging helps you understand what happened, while monitoring helps you understand how your system is performing in real time.

In this article, you will learn step by step how to set up logging and monitoring in Docker containers in simple words with practical examples.

What is Logging in Docker?

Logging means capturing all events and messages generated by your application.

Simple understanding

Think of logs like a diary of your application.

Every action is recorded:

  • Errors

  • Requests

  • System messages

Docker automatically collects logs from containers, which makes debugging easier.

Default behavior in Docker

By default, Docker captures logs written to:

  • Standard Output (stdout)

  • Standard Error (stderr)

Example:

console.log("App started");

Docker will automatically capture this output.

What is Monitoring in Docker?

Monitoring means tracking the health and performance of your containers.

Simple understanding

If logging tells you what happened, monitoring tells you how your system is behaving right now.

What you monitor

  • CPU usage

  • Memory usage

  • Disk I/O

  • Network activity

Monitoring helps you detect problems before users notice them.

Why Logging and Monitoring Are Important

In real-world applications:

  • Containers can crash anytime

  • Applications scale across multiple containers

  • Debugging becomes difficult without visibility

Benefits

  • Faster issue detection

  • Better performance tracking

  • Easier debugging

  • Improved system reliability

Step 1: View Logs Using Docker CLI

Docker provides a simple way to view logs.

docker logs <container_id>

Example

docker logs my-app-container

Useful options

  • Follow logs in real-time:

docker logs -f my-app-container
  • Show last 100 lines:

docker logs --tail 100 my-app-container

This is useful for quick debugging.

Step 2: Use Docker Logging Drivers

Docker supports different logging drivers to store logs in different locations.

Common logging drivers

  • json-file (default)

  • syslog

  • journald

  • fluentd

  • gelf

Example: Using json-file driver

docker run --log-driver json-file my-image

Why logging drivers matter

  • Helps manage log storage

  • Enables integration with external tools

Step 3: Centralized Logging Using ELK Stack

In production, logs from multiple containers need to be centralized.

A popular solution is ELK Stack:

  • Elasticsearch → Stores logs

  • Logstash → Processes logs

  • Kibana → Visualizes logs

Flow

  • Containers generate logs

  • Logstash collects and processes logs

  • Elasticsearch stores them

  • Kibana shows dashboards

Real-world example

If your app is running on 10 containers, ELK helps you see all logs in one place instead of checking each container.

Step 4: Use Fluentd for Log Collection

Fluentd is another popular tool for collecting logs.

Example configuration

docker run --log-driver=fluentd my-image

Why use Fluentd

  • Lightweight

  • Easy integration

  • Supports multiple outputs

Step 5: Monitor Containers Using Docker Stats

Docker provides a built-in command for monitoring.

docker stats

What you see

  • CPU usage

  • Memory usage

  • Network I/O

This is useful for basic monitoring.

Step 6: Use Prometheus and Grafana for Monitoring

For advanced monitoring, Prometheus and Grafana are widely used.

Prometheus

  • Collects metrics from containers

  • Stores time-series data

Grafana

  • Visualizes metrics in dashboards

Flow

  • Prometheus collects data

  • Grafana displays it visually

Example metrics

  • Container CPU usage over time

  • Memory spikes

  • Request rates

Step 7: Use cAdvisor for Container Metrics

cAdvisor is a tool that provides detailed container metrics.

What it tracks

  • CPU usage

  • Memory usage

  • Filesystem usage

Why use it

  • Deep visibility into container performance

  • Works well with Prometheus

Step 8: Combine Logging and Monitoring

In real-world systems, you use both together.

Example scenario

  • Monitoring shows high CPU usage

  • Logs show an error causing infinite loop

Together, they help you quickly find and fix issues.

Best Practices

  • Always log to stdout/stderr

  • Avoid storing logs inside containers

  • Use centralized logging tools

  • Set log rotation to prevent disk issues

  • Monitor key metrics like CPU and memory

Advantages

  • Better visibility into system behavior

  • Faster debugging

  • Improved application performance

  • Scalable monitoring for microservices

Disadvantages

  • Initial setup can be complex

  • Requires additional tools and resources

  • Monitoring tools may consume system resources

Real-World Use Case

Imagine an e-commerce application running in Docker:

  • Logs track user activity and errors

  • Monitoring tracks server load during sales

If traffic spikes:

  • Monitoring alerts high CPU usage

  • Logs help identify slow API calls

This helps teams respond quickly and avoid downtime.

Summary

Logging and monitoring in Docker containers are essential for building reliable and scalable applications. Logging helps you understand what is happening inside your application, while monitoring helps you track system performance in real time. By using tools like Docker logs, logging drivers, ELK stack, Prometheus, and Grafana, you can gain full visibility into your system. When used together, they make debugging easier, improve performance, and ensure smooth operation of containerized applications.