Debugging & Monitoring Docker Containers

Learn how to debug and monitor Docker containers using simple commands.

1. Introduction

Docker makes it easy to run applications, but sometimes containers fail to start, crash, or use too many resources.

Just like checking your car’s dashboard 🚗 to see fuel levels or errors, Docker provides built-in tools to debug and monitor containers.

In this guide, you’ll learn how to check logs, monitor resources, debug failures, and troubleshoot issues using simple commands.


2. Checking Container Logs & Status

Docker logs help you understand what’s happening inside a container.

Viewing Logs of a Running Container

docker logs <container_id>

Example:

docker logs myapp

✅ This shows the container’s output (errors, warnings, or normal activity).

Checking a Container’s Status

docker ps -a

This lists all containers and their status:

CONTAINER ID  IMAGE   STATUS          PORTS
123abc456def  myapp   Exited (1) 2m ago   8080->80/tcp

🚨 If a container has "Exited (1)" status, it means something went wrong!


3. Monitoring Resource Usage with docker stats

To check CPU, memory, and network usage of running containers:

docker stats

Example output:

CONTAINER   CPU %   MEM USAGE / LIMIT   NET I/O
myapp       5.3%    120MB / 512MB       2MB / 1MB

✅ This helps identify high-memory or CPU-consuming containers.


4. Debugging Failed Builds & Crashes

If your docker build command fails, check for errors:

docker build -t myapp .

🚨 Common issues:
Missing files – Ensure all files are in the correct location.
Permission errors – Run with sudo if needed.
Incorrect syntax in Dockerfile – Double-check commands.

To see why a container crashed, check its logs:

docker logs <container_id>

5. Using docker inspect for Troubleshooting

The docker inspect command provides detailed container information.

Check Container Details

docker inspect <container_id>

✅ This shows network settings, volume mounts, and configuration details.


6. Conclusion

Now you can debug & monitor Docker containers like a pro! 🎉

✅ Check logs with docker logs
✅ Monitor resources with docker stats
✅ Debug build failures & crashes
✅ Use docker inspect for detailed info

Try these commands next time you face an issue! 🚀