How to Monitor Docker Containers: Commands and Tips
You can monitor Docker containers using the
docker stats command to see real-time resource usage like CPU and memory. Use docker logs to view container output and troubleshoot issues quickly.Syntax
The main commands to monitor Docker containers are:
docker stats [OPTIONS] [CONTAINER...]: Shows live resource usage statistics for running containers.docker logs [OPTIONS] CONTAINER: Displays the logs (output) of a container.
Options allow filtering or formatting output, such as limiting containers or following logs live.
bash
docker stats [OPTIONS] [CONTAINER...] docker logs [OPTIONS] CONTAINER
Example
This example shows how to monitor a running container named myapp using docker stats and docker logs.
bash
docker stats myapp docker logs --follow myapp
Output
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
abc123def456 myapp 0.15% 50MiB / 2GiB 2.44% 1.2MB / 1.1MB 0B / 0B 5
[logs output streaming live]
Common Pitfalls
Common mistakes when monitoring Docker containers include:
- Not specifying the container name or ID, causing commands to fail or show all containers.
- Using
docker logswithout--followwhen you want live updates. - Ignoring resource limits, which can cause containers to use excessive CPU or memory unnoticed.
Always check container names with docker ps before monitoring.
bash
docker logs myapp
# This shows logs once and exits
docker logs --follow myapp
# This streams logs live, better for real-time monitoringQuick Reference
| Command | Description |
|---|---|
| docker stats [CONTAINER] | Show live CPU, memory, network, and disk usage for containers |
| docker logs CONTAINER | View container output logs |
| docker logs --follow CONTAINER | Stream container logs live |
| docker ps | List running containers with names and IDs |
Key Takeaways
Use
docker stats to monitor real-time resource usage of containers.Use
docker logs --follow to stream container logs live for troubleshooting.Always specify container names or IDs to target specific containers.
Check running containers with
docker ps before monitoring.Monitor resource limits to prevent containers from overusing system resources.