How to use docker container top command to view container processes

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker container top command to inspect the processes running inside a Docker container. You will begin by starting a simple container to have a running environment.

Following that, you will utilize the docker container top command to view the active processes within the container. Finally, you will explore how to use docker container top with various ps options to customize the output and gain more detailed information about the container's processes.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/top("Display Running Processes in Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555125{{"How to use docker container top command to view container processes"}} docker/ps -.-> lab-555125{{"How to use docker container top command to view container processes"}} docker/top -.-> lab-555125{{"How to use docker container top command to view container processes"}} docker/pull -.-> lab-555125{{"How to use docker container top command to view container processes"}} end

Start a simple container

In this step, you will learn how to start a simple Docker container. Docker containers are lightweight, portable, and self-sufficient units that contain everything needed to run an application.

First, let's pull the hello-world image from Docker Hub. This is a very small image that is often used to test if Docker is working correctly.

docker pull hello-world

You should see output indicating that the image is being pulled and extracted.

Now, let's run a container based on the hello-world image.

docker run hello-world

When you run this command, Docker will create a new container from the hello-world image and execute the command defined in the image. The hello-world image is designed to simply print a message to the console and then exit.

You should see output similar to this:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To discover more examples and ideas, visit:
 https://docs.docker.com/get-started/

This output confirms that Docker is installed and working correctly on your LabEx VM. The docker run command created and started a container, which then executed its task and exited.

Use docker container top to view running processes

In this step, you will learn how to use the docker container top command to view the running processes inside a Docker container. This command is similar to the top command on a Linux system, but it shows the processes running specifically within a container.

First, we need a running container. Let's start a simple ubuntu container and keep it running in the background. We will use the -d flag to run the container in detached mode and the sleep infinity command to keep the container alive.

docker run -d ubuntu sleep infinity

You will see a long string of characters printed to the console. This is the container ID.

Now, let's use docker container top followed by the container ID or name to see the processes running inside this container. You can get the container ID by running docker ps.

docker ps

Copy the CONTAINER ID from the output. Then, replace <container_id> with the actual ID in the following command:

docker container top <container_id>

You should see output similar to this, showing the sleep infinity process running inside the container:

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                7345                7331                0                   14:55               ?                   00:00:00            sleep infinity

The output shows information about the processes running within the specified container, including the User ID (UID), Process ID (PID), Parent Process ID (PPID), CPU utilization (C), Start Time (STIME), controlling terminal (TTY), cumulative CPU time (TIME), and the command being executed (CMD). In this case, the main process is sleep infinity.

Use docker container top with ps options

In this step, you will learn how to use the docker container top command with ps options to customize the output and view specific process information within a container. The docker container top command accepts standard ps options, allowing you to control which columns are displayed and in what format.

First, make sure you have a running container from the previous step. You can verify this by running docker ps.

docker ps

Copy the CONTAINER ID of the running ubuntu container.

Now, let's use docker container top with some ps options. For example, to display only the process ID (pid) and the command (cmd), you can use the -o option followed by the desired columns. Replace <container_id> with your container ID.

docker container top pid,cmd < container_id > -o

You should see output similar to this, showing only the PID and CMD columns:

PID                 CMD
7345                sleep infinity

You can use various ps options to get different information. For instance, to see the CPU usage (%cpu) and memory usage (%mem), you can use:

docker container top pid,%cpu,%mem,cmd < container_id > -o

The output will include the CPU and memory usage for the processes in the container:

PID                 %CPU                %MEM                CMD
7345                0.0                 0.0                 sleep infinity

You can refer to the ps command documentation for a full list of available options. Using docker container top with ps options provides a flexible way to monitor the processes running inside your containers and troubleshoot issues.

Summary

In this lab, you learned the fundamental steps of interacting with Docker containers. You began by pulling and running the simple hello-world image, which served as a basic test of your Docker installation and demonstrated the core docker pull and docker run commands. This initial step illustrated how Docker fetches images and creates containers to execute predefined tasks.

Following this, you were introduced to the docker container top command. Although the full content was not provided, the step title indicates that you would learn how to use this command to inspect the processes running inside a container, similar to the top command in a standard Linux environment. The final step, "Use docker container top with ps options," suggests further exploration of docker container top's capabilities, likely demonstrating how to customize the output using options similar to those found in the ps command.