Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Logging and Monitoring Container Metrics docker stats
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-logging-and-monitoring-container-metrics-docker-stats

Docker Docker Logging and Monitoring Container Metrics docker stats

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Monitor container resource usage with docker stats.

2. Step-by-Step

  1. Check if Docker is running: docker ps
  2. Run docker stats with default output: docker stats
  3. Run docker stats with detailed output: docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
  4. Filter by container name: docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" --no-stream
  5. ⚠️ Delete all stopped containers: docker rm $(docker ps -a -q -f status=exited)
  6. Clean up by removing all unused networks: docker network rm $(docker network ls -q -f isolated=true)

3. Real Commands with Examples

  1. Purpose: Monitor CPU usage of a running container.
    Command: docker stats --format "table {{.Name}}\t{{.CPUPerc}}"
    Flags: --format sets the output format, table is the format type, and {{.Name}} and {{.CPUPerc}} are placeholders for the container name and CPU usage percentage, respectively.

  2. Purpose: Monitor memory usage of a running container.
    Command: docker stats --format "table {{.Name}}\t{{.MemUsage}}"
    Flags: Same as above, but with {{.MemUsage}} placeholder for memory usage.

  3. Purpose: Monitor both CPU and memory usage of a running container.
    Command: docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
    Flags: Same as above, but with both {{.CPUPerc}} and {{.MemUsage}} placeholders.

4. Common Errors

  1. Error: docker stats returns an empty output.
    Why: No containers are running.
    Fix: Run docker ps to check if containers are running.

  2. Error: docker stats returns an error message.
    Why: Docker is not running or not properly configured.
    Fix: Run docker --version to check if Docker is installed and running, or docker start to start Docker.

  3. Error: docker stats returns an error message when filtering by container name.
    Why: The container does not exist or is not running.
    Fix: Run docker ps to check if the container is running, or docker ps -a to check if it exists.

  4. Error: docker rm returns an error message when deleting stopped containers.
    Why: The containers are not stopped or do not exist.
    Fix: Run docker ps -a to check if the containers are stopped, or docker ps to check if they exist.

5. Quick Checks

  1. Situation: A container is running and consuming high CPU resources.
    Command: docker stats --format "table {{.Name}}\t{{.CPUPerc}}"
    Correct Response: The container name and CPU usage percentage are displayed.

  2. Situation: A container is running and consuming high memory resources.
    Command: docker stats --format "table {{.Name}}\t{{.MemUsage}}"
    Correct Response: The container name and memory usage percentage are displayed.

  3. Situation: A container is not running.
    Command: docker stats
    Correct Response: An empty output is displayed.

6. Last-Minute Reference

docker stats command is available in Docker 1.13 and later versions.
--format flag is used to set the output format.
table is the default output format.
{{.Name}}, {{.CPUPerc}}, and {{.MemUsage}} are placeholders for container name, CPU usage percentage, and memory usage percentage, respectively.
--no-stream flag is used to disable streaming output.
docker stats command returns an empty output if no containers are running.
docker stats command returns an error message if Docker is not running or not properly configured.
docker rm command deletes all stopped containers by default.
docker network rm command removes all unused networks.
docker ps command lists all running containers.
docker ps -a command lists all containers, including stopped ones.




ADVERTISEMENT