Debugging Docker containers can be challenging, but with the right commands and techniques, you can efficiently troubleshoot issues. Here's a detailed guide on essential Docker debugging commands that every developer should know.
Container Inspection and Logs
docker ps
The most basic debugging starts with checking container status:
docker ps # List running containers
docker ps -a # List all containers, including stopped ones
docker logs
Investigate container logs to understand what's happening inside:
docker logs <container_id> # View container logs
docker logs -f <container_id> # Follow log output in real-time
docker logs --tail 100 <container_id> # View last 100 log lines
docker logs --since 1h <container_id> # View logs from last hour
Interactive Debugging
docker exec
Execute commands inside a running container:
docker exec -it <container_id> /bin/bash # Start interactive shell
docker exec -it <container_id> /bin/sh # For Alpine-based images
docker exec <container_id> ps aux # List running processes
docker inspect
Get detailed information about containers:
docker inspect <container_id> # View all container metadata
docker inspect --format='{{.State.Status}}' <container_id> # Check container status
docker inspect --format='{{.NetworkSettings.IPAddress}}' <container_id> # Get container IP
Resource Monitoring
docker stats
Monitor container resource usage:
docker stats # Monitor all containers
docker stats <container_id> # Monitor specific container
Network Debugging
docker network
Inspect and troubleshoot network connections:
docker network ls # List all networks
docker network inspect <network_name> # Inspect network details
docker network connect <network_name> <container_id> # Connect container to network
Image Debugging
docker history
Understand how an image was built:
docker history <image_name> # View image layer history
docker diff
Check what has changed in container filesystem:
docker diff <container_id> # Show changed files in container
Advanced Debugging Techniques
Using Debug Mode
Enable debug mode for more detailed logs:
dockerd --debug # Start Docker daemon in debug mode
Health Checks
Monitor container health status:
docker inspect --format='{{.State.Health.Status}}' <container_id>
Best Practices for Docker Debugging
Keep containers focused and minimal to reduce debugging complexity
Use appropriate logging drivers and configure log rotation
Implement health checks in your Dockerfile
Monitor resource usage regularly
Maintain proper container naming conventions
Document common debugging procedures for your team
Troubleshooting Common Issues
Container Won't Start
docker events # Monitor Docker events in real-time
docker logs <container_id> # Check startup logs
Network Connectivity Issues
docker network inspect bridge # Inspect default network
docker exec <container_id> ping <destination> # Test connectivity
Remember that preventing issues is often better than debugging them. Implement proper monitoring, logging, and testing strategies in your Docker deployments to catch problems early and maintain healthy container environments.