Docker Troubleshooting
A troubleshooting guide for common Docker problems.
Container Won't Start
# Check the logs
docker logs container_name
# Check the status
docker inspect container_name
# Run for debugging
docker run -it image_name sh
Build Fails
# Build with verbose output
docker build -t myapp . 2>&1 | tee build.log
# Build without cache
docker build --no-cache -t myapp .
# Check the Dockerfile
cat Dockerfile
Network Issues
# Check the network
docker network inspect bridge
# Check port mapping
docker port container_name
# Test the connection
docker exec container_name ping google.com
Volume Issues
# Check the volume
docker volume inspect volume_name
# Check the mount point
docker inspect container_name | grep Mounts
# Check permissions
docker exec container_name ls -la /path
Use Case: Troubleshooting
# Problem: Container crashes
docker logs api
# Error: EACCES: permission denied
# Solution: Fix the permissions
docker run --rm -v $(pwd):/app -w /app node:18 chown -R node:node /app
# Problem: Port conflict
docker run -d -p 80:80 nginx
# Error: Bind for 0.0.0.0:80 failed: port is already allocated
# Solution: Check which port is in use
lsof -i :80
# or use a different port
docker run -d -p 8080:80 nginx
Common Errors
"No space left on device"
# Cleanup
docker system prune -a --volumes
# Check disk usage
docker system df
"Cannot connect to Docker daemon"
# Start Docker
sudo systemctl start docker
# Check the status
sudo systemctl status docker
"Image not found"
# Check the image
docker images | grep myapp
# Rebuild
docker build -t myapp .
Everyday Use Case
Case: A developer runs into an error when running a container.
# Debug the container
docker run -it --rm myapp sh
# Check the environment
docker exec api env
# Check the network
docker network inspect bridge
Log Analysis
# Search for errors
docker logs api 2>&1 | grep -i error
# Export the logs
docker logs api > api.log 2>&1
# View the latest logs
docker logs --tail 100 api
Best Practices
- Check the logs first:
docker logs container_name - Use --rm: For debugging containers
- Check resources:
docker stats - Read the documentation: Docker docs for specific errors
- Community: Stack Overflow, Docker forums
Conclusion
Troubleshooting Docker requires a systematic approach. Check the logs, inspect the container, and use debugging tools to resolve the problem.