🐳 Docker Operations: Your Container Control Center
Imagine you’re the captain of a big ship. The ship has lots of rooms (containers), engines (resources), and crew members (processes). As captain, you need to know what’s happening everywhere, clean up messes, and sometimes control the ship from far away. That’s exactly what Docker Operations is about!
🎯 What You’ll Learn
Think of Docker Operations as your super-powered remote control for all things Docker. You’ll learn how to:
- Talk to Docker like a walkie-talkie (REST API)
- Listen to what Docker is doing (Events)
- Check the ship’s health (System Management)
- Clean up the mess (Resource Cleanup)
- Remove old photos nobody wants (Dangling Images)
- Switch between different ships (Docker Context)
- Control ships from far away (Remote Docker Hosts)
📡 Docker REST API: The Walkie-Talkie
What Is It?
Imagine Docker has a secret phone number. When you call this number, you can ask Docker to do things—start containers, stop them, or just check how they’re doing. This “phone” is called the REST API.
Simple Example:
- You → API: “Hey Docker, what containers are running?”
- Docker → You: “Container A, Container B, Container C!”
How It Works
Docker listens on a special address. You send messages (HTTP requests), and Docker answers back.
# Ask Docker about running containers
curl --unix-socket /var/run/docker.sock \
http://localhost/containers/json
Real-Life Use:
- Automation tools (like Jenkins) use the API to build and deploy
- Monitoring dashboards fetch container stats
- Custom scripts control Docker without typing commands
Quick Diagram
graph TD A["Your App"] -->|HTTP Request| B["Docker API"] B -->|Response| A B --> C["Containers"] B --> D["Images"] B --> E["Networks"]
👂 Docker Events: The Announcer
What Is It?
Imagine a loudspeaker in your ship that announces every time something happens: “Container started!”, “Image downloaded!”, “Network created!”
That’s Docker Events—a live stream of everything happening in Docker.
Simple Example:
# Listen to all Docker announcements
docker events
Now start a container in another window:
docker run hello-world
You’ll see announcements like:
container create abc123...
container start abc123...
container die abc123...
Why Is This Cool?
- Monitoring: Know when something crashes
- Automation: Trigger actions when events happen
- Debugging: See the exact order of what happened
Filter Events
Only want to hear about containers?
docker events --filter type=container
Only care about the last hour?
docker events --since 1h
🔧 Docker System Management: Health Check
What Is It?
Like a doctor checking your health, Docker system commands tell you how healthy your Docker is.
The Commands
1. See Everything at Once:
docker system info
This shows:
- How many containers, images, volumes
- What storage driver you use
- How much memory Docker can use
2. Check Space Usage:
docker system df
Output looks like:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 15 5 2.5GB 1.8GB (72%)
Containers 8 3 500MB 200MB (40%)
Volumes 10 4 1GB 600MB (60%)
Translation: “You have 15 images, but only 5 are being used. You can free up 1.8GB!”
3. See Detailed Breakdown:
docker system df -v
Shows every image and container with their sizes.
🧹 Docker Resource Cleanup: Spring Cleaning
The Problem
Docker is like a kid who never throws anything away. Old containers, unused images, forgotten networks—they pile up!
The Solution: Prune Commands
Clean Everything at Once:
docker system prune
This removes:
- ✅ Stopped containers
- ✅ Unused networks
- ✅ Dangling images
- ✅ Build cache
Want to include volumes too?
docker system prune --volumes
Skip the “Are you sure?” question:
docker system prune -f
Individual Cleanup Commands
| What to Clean | Command |
|---|---|
| Stopped containers | docker container prune |
| Unused images | docker image prune |
| Unused volumes | docker volume prune |
| Unused networks | docker network prune |
Before & After Example
# Before: 5GB used
docker system df
# Clean up
docker system prune -a --volumes
# After: 500MB used 🎉
docker system df
🖼️ Dangling Images: The Orphan Photos
What Are They?
Imagine you take a photo, then edit it and save a new version. The old version is still on your phone, but it has no name—just takes up space.
Dangling images are the same: old image layers that no longer belong to any named image.
How They Happen
When you rebuild an image with the same tag:
docker build -t myapp:latest .
# ...make changes...
docker build -t myapp:latest .
The old myapp:latest becomes a dangling image (shown as <none>:<none>).
Find Them
docker images -f dangling=true
Output:
REPOSITORY TAG IMAGE ID SIZE
<none> <none> abc123def 500MB
<none> <none> xyz789ghi 300MB
Remove Them
# Remove only dangling images
docker image prune
# Remove ALL unused images (not just dangling)
docker image prune -a
Quick Diagram
graph TD A["Build myapp:v1"] --> B["myapp:v1 exists"] B --> C["Rebuild myapp:v1"] C --> D["New myapp:v1"] C --> E["Old image = Dangling"] E --> F["docker image prune"] F --> G["Gone! Space freed"]
🎯 Docker Context: Multiple Remote Controls
What Is It?
Imagine you have three TVs in different rooms. Instead of walking to each one, you have a universal remote that can switch between them.
Docker Context lets you switch between different Docker environments without changing anything else.
Real-World Use Cases
- Your laptop (local Docker)
- A development server
- A production server
- Docker in the cloud
Commands
See all contexts:
docker context ls
Create a new context:
docker context create my-server \
--docker "host=ssh://user@192.168.1.100"
Switch to it:
docker context use my-server
Now all commands go to that server:
docker ps # Shows containers on my-server!
Switch back to local:
docker context use default
Quick Diagram
graph TD A["Your Computer"] --> B{Docker Context} B -->|default| C["Local Docker"] B -->|my-server| D["Remote Server 1"] B -->|prod| E["Production Server"]
🌐 Remote Docker Hosts: Control from Anywhere
What Is It?
Instead of sitting at the ship’s wheel, you can steer from home! Remote Docker hosts let you run Docker commands on a machine far away.
Method 1: SSH Connection
The easiest and safest way:
# One-time command
docker -H ssh://user@remote-server ps
# Or set environment variable
export DOCKER_HOST=ssh://user@remote-server
docker ps # Now goes to remote!
Method 2: TCP (Be Careful!)
⚠️ Warning: Only use on private networks!
On the remote server, Docker listens on a port:
# Remote server's Docker config
dockerd -H tcp://0.0.0.0:2375
On your computer:
export DOCKER_HOST=tcp://remote-server:2375
docker ps
Method 3: TLS for Security
For secure TCP connections:
export DOCKER_HOST=tcp://remote:2376
export DOCKER_TLS_VERIFY=1
export DOCKER_CERT_PATH=/path/to/certs
docker ps # Encrypted & authenticated!
Comparison Table
| Method | Security | Setup | Best For |
|---|---|---|---|
| SSH | ⭐⭐⭐ High | Easy | Most cases |
| TCP | ⭐ Low | Very Easy | Testing only |
| TLS | ⭐⭐⭐ High | Complex | Production |
🎬 Putting It All Together
Let’s see a day in the life of a Docker captain:
# Morning: Check the fleet status
docker context use production
docker system info
# Afternoon: Something's slow, investigate
docker events --since 1h
docker system df
# Evening: Clean up the mess
docker system prune -a --volumes
# Night: Check dev server from home
docker context use development
docker ps
🧠 Key Takeaways
| Topic | One-Line Summary |
|---|---|
| REST API | Docker’s phone number for automation |
| Events | Live announcements of everything happening |
| System Management | Health check for your Docker |
| Resource Cleanup | Spring cleaning with prune commands |
| Dangling Images | Orphan photos that need deleting |
| Docker Context | Universal remote for multiple Dockers |
| Remote Hosts | Control Docker from anywhere |
🚀 You Did It!
You’re now a Docker Operations expert! You can:
- ✅ Talk to Docker through its API
- ✅ Listen to real-time events
- ✅ Check system health
- ✅ Clean up like a pro
- ✅ Switch between multiple Docker environments
- ✅ Control Docker from anywhere in the world
Remember: A good captain keeps their ship clean and knows what’s happening everywhere. Now you can do the same with Docker! 🐳
