Common Docker Debugging Scenarios
1. Container fails to start.
2. Application crashes inside a running container.
3. Unexpected behavior or incorrect output.
4. Resource usage issues affecting container performance.
5. Network connectivity problems between containers or external systems.

Debugging Tools and Methods

1. Viewing Logs:
Logs are the first place to check for errors or warnings:

docker logs container-name-or-id

Follow logs in real-time using the `-f` flag:

docker logs -f container-name-or-id

2. Inspecting Container Details:
Get detailed information about a container’s configuration and state:

docker inspect container-name-or-id

Look for error messages or misconfigurations in the output.

3. Accessing the Container Shell:
Open an interactive shell inside the container to explore its file system and runtime state:

docker exec -it container-name-or-id bash

If the container uses a minimal image like Alpine, replace `bash` with `sh`:

docker exec -it container-name-or-id sh

4. Checking Resource Usage:
Monitor resource consumption for running containers:

docker stats

This displays real-time CPU, memory, network, and I/O usage.

5. Testing Networking:
Check connectivity between containers or external systems using network utilities like `ping` or `curl`:

docker exec -it container-name-or-id ping target-container-or-host
docker exec -it container-name-or-id curl http://example.com

6. Viewing Container Events:
Track events related to a container, such as start, stop, or error events:

docker events --filter container=container-name-or-id

7. Running Containers in Debug Mode:
Enable debug logs for the Docker daemon by editing the `daemon.json` file:

{
  "debug": true
}

Restart the Docker service to apply the changes:

sudo systemctl restart docker

8. Debugging Build Issues:
Use the `--no-cache` option to rebuild images from scratch:

docker build --no-cache -t my-image .

Check the build output for errors or warnings.

Debugging Best Practices
1. Keep Logs Accessible: Redirect container logs to external storage for easier access and analysis.
2. Use Health Checks: Define health checks in your Dockerfile to detect and handle issues automatically:

HEALTHCHECK CMD curl --fail http://localhost:8080 || exit 1

3. Run Containers as Non-Root Users: This improves security and reduces the risk of accidental misconfigurations.
4. Limit Resource Allocation: Prevent resource-related issues by setting CPU and memory limits for containers:

docker run --memory="512m" --cpus="1.0" my-image

5. Use Tools for Advanced Debugging: Tools like `Sysdig`, `cAdvisor`, and `Dive` can provide deeper insights into container behavior and resource usage.

Common Debugging Tools
1. Sysdig: Monitor system calls and container activity.
2. cAdvisor: Analyze resource usage metrics.
3. Dive: Explore and analyze Docker image layers.
4. Wireshark: Capture and analyze network traffic between containers.

Conclusion
Debugging Docker containers requires a combination of built-in tools, advanced techniques, and best practices. By mastering these methods, you can identify and resolve issues quickly, ensuring the stability and performance of your containerized applications. Start applying these debugging strategies to enhance your container management skills today.