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.