đ§ Docker Troubleshooting: Become the Container Detective
Imagine youâre a detective. Your containers are like little rooms in a building. Sometimes things go wrong inside those roomsâlights go out, doors get stuck, or pipes leak. Your job? Find out whatâs broken and fix it!
đŻ What Weâll Learn
Today, weâll become Docker Detectives and learn how to:
- Debug containers (peek inside the rooms)
- Fix common errors (know what breaks and why)
- Troubleshoot networks (check the hallways and pipes)
- Read logs (find clues in the diary)
đ Container Debugging Techniques
The Detectiveâs Toolkit
Think of debugging like being a doctor for your containers. When a patient (container) feels sick, you need tools to check whatâs wrong!
Technique 1: Peek Inside with docker exec
This is like opening the door and walking into the room to look around.
docker exec -it mycontainer /bin/sh
What this does:
exec= âexecute a command insideâ-it= âlet me type and see thingsâ/bin/sh= âopen a terminalâ
Example:
# Check what files are inside
docker exec -it web ls /app
# See running processes
docker exec -it web ps aux
Technique 2: Watch Whatâs Happening with docker logs
Containers keep a diary of everything they do. Reading it tells you what went wrong!
docker logs mycontainer
Pro Tips:
# See only last 50 lines
docker logs --tail 50 mycontainer
# Watch live updates
docker logs -f mycontainer
Technique 3: Health Check with docker inspect
This is like an X-ray machineâyou see EVERYTHING about the container.
docker inspect mycontainer
Example - Find the IP address:
docker inspect mycontainer | grep IPAddress
Technique 4: See Resource Usage
Is your container eating too much memory or CPU?
docker stats mycontainer
This shows live usage like a heartbeat monitor!
â ïž Common Docker Errors (And How to Fix Them)
The âUsual Suspectsâ of Docker Problems
Letâs meet the villains and learn how to defeat them!
đ« Error 1: âContainer Exits Immediatelyâ
What you see:
Container exited with code 0
Why it happens: The containerâs main job finished instantly. Like a worker who shows up, sees nothing to do, and leaves!
The Fix:
# Keep it running with a command
docker run -d myimage tail -f /dev/null
# Or check what command it runs
docker inspect myimage --format='{{.Config.Cmd}}'
đ« Error 2: âPort Already in Useâ
What you see:
Bind for 0.0.0.0:8080 failed:
port is already allocated
Why it happens: Another container (or program) is already using that door!
The Fix:
# Find who's using the port
docker ps | grep 8080
# Or use a different port
docker run -p 8081:80 myimage
đ« Error 3: âNo Space Left on Deviceâ
What you see:
no space left on device
Why it happens: Your disk is full of old images and containers!
The Fix:
# Clean up everything unused
docker system prune -a
# See what's taking space
docker system df
đ« Error 4: âImage Not Foundâ
What you see:
Error: pull access denied
repository does not exist
Why it happens: You typed the name wrong, or the image doesnât exist.
The Fix:
# Check spelling
docker search nginx
# Make sure you're logged in
docker login
đ« Error 5: âPermission Deniedâ
What you see:
permission denied while connecting
to Docker daemon socket
Why it happens: You need special permission to talk to Docker!
The Fix:
# Add yourself to docker group
sudo usermod -aG docker $USER
# Then log out and back in!
đ Network Troubleshooting
When Containers Canât Talk to Each Other
Imagine containers as houses in a neighborhood. They need roads (networks) to visit each other!
Step 1: Check What Networks Exist
docker network ls
Youâll see something like:
NETWORK ID NAME DRIVER
abc123 bridge bridge
def456 host host
Step 2: See Whoâs Connected
docker network inspect bridge
This shows all containers on that network!
Step 3: Test Connection Between Containers
Can container A talk to container B?
# Get inside container A
docker exec -it containerA /bin/sh
# Try to ping container B
ping containerB
Step 4: Common Network Fixes
Problem: Containers canât find each other by name
Solution: Put them on the same custom network!
# Create a network
docker network create mynetwork
# Run containers on it
docker run -d --network mynetwork \
--name web nginx
docker run -d --network mynetwork \
--name api myapi
# Now 'web' can reach 'api' by name!
Quick Network Diagram
graph TD A["Container: web"] --> N["mynetwork"] B["Container: api"] --> N C["Container: db"] --> N N --> D["Can talk by name!"]
đ Log Analysis for Debugging
Reading the Containerâs Diary
Logs are like a detectiveâs best friendâthey tell you EXACTLY what happened and when!
Basic Log Commands
# See all logs
docker logs mycontainer
# See last 100 lines
docker logs --tail 100 mycontainer
# See logs from last 10 minutes
docker logs --since 10m mycontainer
# Watch logs live
docker logs -f mycontainer
# Show timestamps
docker logs -t mycontainer
Finding Errors in Logs
Use grep to search for problems:
# Find all errors
docker logs mycontainer 2>&1 | grep -i error
# Find warnings
docker logs mycontainer 2>&1 | grep -i warn
# Find crashes
docker logs mycontainer 2>&1 | grep -i fatal
Understanding Log Levels
Logs use these words to tell you how serious things are:
| Level | Meaning | Action |
|---|---|---|
| DEBUG | Extra details | Usually ignore |
| INFO | Normal updates | Good news! |
| WARN | Something odd | Check it out |
| ERROR | Something broke | Fix it soon! |
| FATAL | Container died | Fix it NOW! |
Example: Debugging a Crash
Your container keeps restarting. Hereâs how to investigate:
# Step 1: See the logs
docker logs --tail 200 mycontainer
# Step 2: Look for the error
docker logs mycontainer 2>&1 | grep -i error
# Step 3: Check when it crashed
docker logs -t mycontainer | tail -20
Sample error you might find:
2024-01-15T10:30:45 ERROR:
Cannot connect to database at db:5432
The clue: Container canât reach the database. Check the network!
đŻ Quick Reference: The Debugging Checklist
When something goes wrong, follow these steps:
-
Is the container running?
docker ps -a -
What do the logs say?
docker logs mycontainer -
Whatâs the containerâs status?
docker inspect mycontainer -
Can it reach the network?
docker exec mycontainer ping google.com -
Is there enough disk space?
docker system df
đ Youâre Now a Docker Detective!
Youâve learned to:
- â
Peek inside containers with
exec - â
Read the diary with
logs - â
Diagnose problems with
inspect - â Fix common errors quickly
- â Debug networks between containers
- â Analyze logs like a pro
Remember: Every expert was once a beginner. The more you debug, the faster youâll spot problems. Youâve got this! đ
âThe best debugger is a calm mind and good logs.â â Every DevOps Engineer Ever
