π 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:
- π Logs - Read the containerβs diary
- π Log Drivers - Choose where diaries are stored
- π¬ Inspection - X-ray vision for containers
- π Stats - Real-time health dashboard
- πͺ 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!