DOCKER - 12 View activities in a container
Today we run Docker commands to understand what's happening inside a running container. There are several ways to look in: the top command lists the processes, inspect shows startup and configuration metadata, and stats streams live performance data for every container. To experiment, we'll start two containers — an Nginx and a MySQL with a random root password:
docker container run -d --name nginx nginx
docker container run -d --name mysql \
-e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
docker container ls
Three windows into a container
The first command, docker container top <name>, lists the processes running inside a specific container. Run it against mysql or nginx and you'll see exactly what each one is doing under the hood. The second command, docker container inspect mysql, returns a JSON array packed with metadata about how the container was started, its mounts, network settings, environment variables and image references — everything that defines its configuration.
That metadata is useful, but it says nothing about what's happening right now. For runtime insight, use docker container stats — without arguments it opens a streaming view of every container, updating CPU, memory, network and block I/O every few seconds. The default output shows the container ID rather than the name, so a quick docker container ls in another tab makes matching easier. Press Ctrl+C to leave the live view.
docker container top <name>— list internal processesdocker container inspect <name>— full JSON of startup configdocker container stats— live CPU/RAM/IO feed for all containers
These three commands are essential everyday tools: top to peek at what's running, inspect to audit how a container was started, and stats to spot any container that's eating too much RAM or bandwidth on the host.
Summary
This lesson covers three essential Docker commands for monitoring container activities: 'docker containers top' to list processes running inside a container, 'docker containers inspect' to view detailed startup configuration and metadata, and 'docker containers stats' to display real-time CPU and memory performance metrics. The instructor demonstrates each command with practical examples using nginx and MySQL containers, showing how to monitor resource consumption effectively on local systems.
Key points
- Use 'docker containers top <container-name>' to list all processes currently running inside a specific container
- Use 'docker containers inspect <container-name>' to view complete container configuration details, environment variables, and startup metadata in JSON format
- Use 'docker containers stats' to monitor real-time CPU and memory resource usage across all containers in a continuous stream
- The stats command displays container IDs but not names—use 'docker ps' alongside it to match IDs with container names
- Resource monitoring is essential for ensuring containers do not consume excessive memory or CPU on local machines
FAQ
How can you view the processes running inside a Docker container?
Run the command 'docker containers top <container-name>' to list all processes currently executing within that specific container.
What does 'docker containers inspect' command show and why is it useful?
It displays detailed container metadata including startup configuration, environment variables, and all settings used when the container was launched—useful for understanding how the container is configured and what options were applied.
How do you monitor container resource usage like CPU and memory in real-time?
Use 'docker containers stats' (with optional container name) to view a continuous stream of real-time performance data showing CPU percentage and memory usage for your containers.