Container Monitoring

Loading concept...

πŸ” Container Monitoring: Becoming a Docker Detective

The Story of the Watchful Guardian

Imagine you’re the guardian of a magical kingdom where tiny houses (containers) pop up and disappear. Your job? Keep an eye on everything! Know when a house is healthy, when it’s sick, and what happened before it vanished.

That’s Container Monitoring! It’s like being a detective who watches over all your Docker containers, making sure they’re happy and healthy.


🏠 The Five Superpowers of a Container Detective

graph TD A[πŸ” Container Monitoring] --> B[πŸ“œ Container Logs] A --> C[🚚 Log Drivers] A --> D[πŸ”¬ Container Inspection] A --> E[πŸ“Š Stats & Metrics] A --> F[πŸšͺ Exit Codes] style A fill:#667eea,color:#fff style B fill:#4ECDC4,color:#fff style C fill:#FF6B6B,color:#fff style D fill:#45B7D1,color:#fff style E fill:#96CEB4,color:#fff style F fill:#FFEAA7,color:#333

πŸ“œ Container Logs: The Container’s Diary

What Are Container Logs?

Think of logs like a diary. Every time your container does something, it writes it down!

  • Good news? Written in the diary.
  • Error happened? Written in the diary.
  • Someone connected? Written in the diary.

Reading the Diary

# See all diary entries
docker logs my-container

# Watch diary entries live!
docker logs -f my-container

# Last 10 entries only
docker logs --tail 10 my-container

# Show timestamps (when it happened)
docker logs -t my-container

Real Life Example

# Your web app container's diary
docker logs web-app

# Output might look like:
# [INFO] Server started on port 3000
# [INFO] User logged in: alice
# [ERROR] Database timeout!

Why it matters: When something breaks, the logs tell you what happened!


🚚 Log Drivers: Where Does the Diary Go?

The Problem

Your container writes a diary. But where should it be stored?

  • On your computer?
  • Sent to a cloud service?
  • Written to a special system?

Log Drivers = Delivery Trucks

Think of log drivers as different trucks that carry your diary entries to different places!

graph TD A[πŸ“œ Container Logs] --> B[🚚 json-file] A --> C[🚚 syslog] A --> D[🚚 journald] A --> E[🚚 awslogs] A --> F[🚚 none] B --> G[πŸ“ Local File] C --> H[πŸ“‘ Syslog Server] D --> I[🐧 Linux Journal] E --> J[☁️ AWS CloudWatch] F --> K[πŸ—‘οΈ Nowhere] style A fill:#667eea,color:#fff

Common Log Drivers

Driver Where Logs Go Use When
json-file Local JSON files Default, simple apps
syslog Syslog server Enterprise systems
journald Linux journal Linux servers
awslogs AWS CloudWatch Running on AWS
none Nowhere! When you don’t need logs

Setting a Log Driver

# Use syslog driver
docker run --log-driver=syslog \
  my-container

# Use json-file with size limit
docker run --log-driver=json-file \
  --log-opt max-size=10m \
  --log-opt max-file=3 \
  my-container

Pro Tip: json-file is the default. It stores logs on your machine in JSON format!


πŸ”¬ Container Inspection: X-Ray Vision

What Is It?

docker inspect is like having X-ray vision. You can see EVERYTHING about a container:

  • What image it uses
  • Its network settings
  • Environment variables
  • Mounted folders
  • And much more!

The Magic Command

# See EVERYTHING
docker inspect my-container

# That's too much! Let's filter:

# Just the IP address
docker inspect -f \
  '{{.NetworkSettings.IPAddress}}' \
  my-container

# Just the state
docker inspect -f \
  '{{.State.Status}}' my-container

What You Can Find

graph TD A[πŸ”¬ docker inspect] --> B[🌐 Network Info] A --> C[πŸ’Ύ Mount Points] A --> D[βš™οΈ Environment Vars] A --> E[πŸ“¦ Image Details] A --> F[πŸƒ Running State] style A fill:#45B7D1,color:#fff

Real Example

# Find container's IP address
docker inspect -f \
  '{{.NetworkSettings.IPAddress}}' \
  web-server

# Output: 172.17.0.2

Think of it like this: If docker logs tells you what happened, docker inspect tells you what the container IS!


πŸ“Š Container Stats and Metrics: The Health Dashboard

What Are Stats?

Just like your phone shows battery and storage, docker stats shows you:

  • CPU usage - How hard the brain is working
  • Memory - How much space is used
  • Network - Data flowing in and out
  • Disk I/O - Reading and writing files

Watching Stats Live

# Live dashboard for ALL containers
docker stats

# Just one container
docker stats my-container

# One snapshot (no live updates)
docker stats --no-stream

What You See

CONTAINER   CPU%    MEM USAGE/LIMIT   NET I/O       BLOCK I/O
web-app     2.50%   150MB / 1GB       5MB / 2MB     10MB / 5MB
database    15.3%   500MB / 2GB       1MB / 500KB   50MB / 20MB

Understanding the Numbers

Metric What It Means Warning Signs
CPU % Processor usage Over 80% = slow
Memory RAM used Near limit = crashes
Net I/O Network traffic Unusually high = problems
Block I/O Disk activity High = slow storage
graph TD A[πŸ“Š docker stats] --> B[🧠 CPU Usage] A --> C[πŸ’Ύ Memory Usage] A --> D[🌐 Network I/O] A --> E[πŸ’Ώ Disk I/O] B --> F{Too High?} C --> G{Near Limit?} F --> H[⚠️ Slow App] G --> I[πŸ’₯ Crash Risk] style A fill:#96CEB4,color:#fff style H fill:#FF6B6B,color:#fff style I fill:#FF6B6B,color:#fff

πŸšͺ Exit Codes: The Final Message

What Are Exit Codes?

When a container stops, it leaves behind a number. This number tells you WHY it stopped!

Think of it like a goodbye note:

  • 0 = β€œI finished my job perfectly! Bye!”
  • Other numbers = β€œSomething went wrong…”

Common Exit Codes

Code Meaning What Happened
0 Success! Container finished normally
1 General error Something went wrong
137 Killed (SIGKILL) Docker forced it to stop
139 Segmentation fault Memory problem
143 Terminated (SIGTERM) Graceful shutdown requested

Finding the Exit Code

# See exit code of stopped container
docker inspect -f \
  '{{.State.ExitCode}}' my-container

# Or check container status
docker ps -a
# Shows "Exited (0)" or "Exited (1)"

Exit Code Detective Work

graph TD A[Container Stopped] --> B{Exit Code?} B --> C[0 = Success βœ…] B --> D[1 = Error ❌] B --> E[137 = Killed πŸ’€] B --> F[139 = Crash πŸ’₯] B --> G[143 = Stopped πŸ›‘] D --> H[Check Logs!] E --> I[Memory Limit?] F --> J[Code Bug?] style A fill:#667eea,color:#fff style C fill:#4ECDC4,color:#fff style D fill:#FF6B6B,color:#fff style E fill:#FF6B6B,color:#fff style F fill:#FF6B6B,color:#fff style G fill:#FFEAA7,color:#333

Real Example

# Container crashed - why?
docker inspect -f \
  '{{.State.ExitCode}}' broken-app

# Output: 137
# Meaning: Docker killed it (probably
# ran out of memory!)

🎯 Putting It All Together

When something goes wrong, follow this detective process:

graph TD A[🚨 Problem!] --> B[Check Exit Code] B --> C[Read Logs] C --> D[Check Stats] D --> E[Inspect Container] E --> F[πŸŽ‰ Found the Issue!] style A fill:#FF6B6B,color:#fff style F fill:#4ECDC4,color:#fff

The Detective’s Toolkit

# Step 1: What's the exit code?
docker inspect -f \
  '{{.State.ExitCode}}' my-app

# Step 2: What do the logs say?
docker logs --tail 50 my-app

# Step 3: What are the stats?
docker stats my-app --no-stream

# Step 4: Deep inspection
docker inspect my-app

🌟 Quick Reference

Task Command
View logs docker logs container
Follow logs live docker logs -f container
Set log driver --log-driver=driver
Inspect container docker inspect container
View stats docker stats
Get exit code Check docker ps -a

πŸš€ You’re Now a Container Detective!

You’ve learned the five superpowers:

  1. πŸ“œ Logs - Read the container’s diary
  2. 🚚 Log Drivers - Choose where diaries are stored
  3. πŸ”¬ Inspection - X-ray vision for containers
  4. πŸ“Š Stats - Real-time health dashboard
  5. πŸšͺ Exit Codes - Understand goodbye messages

With these powers, no container mystery can escape you!

Remember: When something breaks, check the exit code β†’ read the logs β†’ check the stats β†’ inspect everything. You’ll find the problem every time!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.