By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Monitor container resource usage with docker stats.
docker ps
docker stats
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" --no-stream
docker rm $(docker ps -a -q -f status=exited)
docker network rm $(docker network ls -q -f isolated=true)
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.
docker stats --format "table {{.Name}}\t{{.CPUPerc}}"
--format
table
{{.Name}}
{{.CPUPerc}}
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.
docker stats --format "table {{.Name}}\t{{.MemUsage}}"
{{.MemUsage}}
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.
Error: docker stats returns an empty output. Why: No containers are running. Fix: Run docker ps to check if containers are running.
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.
docker --version
docker start
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.
docker ps -a
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.
docker rm
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.
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.
Situation: A container is not running. Command: docker stats Correct Response: An empty output is displayed.
• 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.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.