DOCKER - 12 View activities in a container
Today we run a few Docker commands to understand what is happening inside a running container. We have already seen the top command, which shows the processes running in a container, but Docker offers other useful commands. inspect exposes how the container was started and all its metadata, while stats streams CPU, memory and I/O for each running container in real time.
To play with these commands we first need running containers. Let's start an nginx container and a MySQL container:
docker container run -d --name nginx nginx
docker container run -d --name mysql -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql
docker container ls
Three commands to investigate a container
- docker container top <name> — lists the processes currently running inside the container (for example
docker container top mysql). - docker container inspect <name> — returns a large JSON document describing how the container was started: image, command, env, mounts, network configuration. Great for debugging configuration, less useful for live activity.
- docker container stats — opens a streaming, top-like view of CPU, memory, network I/O and block I/O for all running containers. You can also narrow it down to a specific container by passing its name or ID.
The stats command unfortunately shows the container ID rather than its name, so a quick docker container ls in another tab is handy to match IDs with names. This is especially useful on a local laptop where you want to make sure a container is not silently exhausting RAM, disk or bandwidth. Press Ctrl+C to leave the live view. See you in the next video.
Summary
This lesson teaches Docker commands for monitoring and observing activities inside running containers. It covers three essential commands: `docker container top` to list running processes, `docker inspect` to view detailed container configuration and metadata, and `docker container stats` to display real-time performance metrics (CPU, memory) for all containers. These monitoring tools help developers ensure containers are performing efficiently and not consuming excessive system resources.
Key points
- Use `docker container top <container>` to list all processes currently running inside a specific container, similar to viewing a process list
- Use `docker inspect <container>` to view comprehensive startup configuration, environment variables, and metadata about how the container was launched
- `docker container stats` displays real-time performance metrics in a continuous stream, showing CPU and memory usage for all running containers
- The `stats` command is particularly useful on local machines to monitor resource consumption and prevent unmonitored processes from filling disk space or consuming excessive bandwidth
- You can specify a container name with `docker stats` to view data for just that container, or omit it to see statistics for all running containers simultaneously
- The `inspect` command provides detailed data about container configuration but does not show real-time activity, whereas `stats` focuses specifically on live performance metrics
FAQ
What is the difference between `docker top` and `docker inspect`?
`docker top` lists the currently running processes inside a container (like a process manager view), while `docker inspect` shows the detailed configuration and metadata of how the container was set up at startup, including environment variables and startup options.
How is `docker stats` useful in practical scenarios?
`docker stats` provides real-time performance monitoring to ensure containers are not consuming excessive CPU, memory, or disk resources, which is critical on local machines to prevent unmonitored processes from causing system issues.
Can you monitor a specific container with `docker stats`?
Yes, you can specify a container name with `docker stats` to see real-time data for just that container, or leave it empty to view statistics for all running containers simultaneously in a live-updating view.