{"id":48416,"date":"2025-02-08T08:28:26","date_gmt":"2025-02-08T08:28:26","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=48416"},"modified":"2025-07-12T05:38:43","modified_gmt":"2025-07-12T05:38:43","slug":"docker-top","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/docker-top\/","title":{"rendered":"Docker commands Guide &#8211; docker top with examples"},"content":{"rendered":"\n<p>Here\u2019s a <strong>complete tutorial<\/strong> on <code>docker top<\/code>, covering <strong>what it does<\/strong>, <strong>examples<\/strong>, and <strong>use cases<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is <code>docker top<\/code>?<\/strong><\/h2>\n\n\n\n<p><code>docker top<\/code> is a Docker command used to <strong>display the running processes<\/strong> inside a container. It shows information similar to the <strong><code>ps<\/code> command<\/strong> in Linux, helping you <strong>monitor and troubleshoot<\/strong> the container\u2019s processes in real time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Features:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Lists all <strong>running processes<\/strong> inside a container.<\/li>\n\n\n\n<li>Helps in <strong>debugging and monitoring<\/strong> process-level activity.<\/li>\n\n\n\n<li>Supports <strong>custom process options<\/strong> like <code>ps<\/code> flags (<code>-ef<\/code> or <code>aux<\/code>).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic Syntax<\/strong><\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">docker<\/span> <span class=\"hljs-selector-tag\">top<\/span> <span class=\"hljs-selector-tag\">CONTAINER<\/span> <span class=\"hljs-selector-attr\">&#91;ps OPTIONS]<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Arguments:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>CONTAINER<\/code>: The name or ID of the container.<\/li>\n\n\n\n<li><code>[ps OPTIONS]<\/code>: Optional <strong>Linux <code>ps<\/code> options<\/strong> to customize the output (e.g., <code>-ef<\/code>, <code>aux<\/code>).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Examples of <code>docker top<\/code><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. List Running Processes in a Container<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker top my_container\n<\/code><\/span><\/pre>\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">UID     PID   PPID  C  STIME  TTY   TIME   CMD\nroot    123   1     0  10:00  ?     00:00 \/bin\/bash\nroot    456   123   0  10:01  ?     00:00 sleep 300\n<\/code><\/span><\/pre>\n\n\n<p>This shows all running processes in <code>my_container<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Use <code>ps -ef<\/code> to Get Detailed Process Information<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker top my_container -ef\n<\/code><\/span><\/pre>\n\n\n<p>This displays detailed process information with <strong>extended fields<\/strong>, including <strong>UID, PID, CPU, and start time<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Display Process Information Similar to <code>ps aux<\/code><\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker top my_container aux\n<\/code><\/span><\/pre>\n\n\n<p>This provides process information in a format similar to the <strong><code>ps aux<\/code><\/strong> command.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Check the Main Process of a Container<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker top my_web_container\n<\/code><\/span><\/pre>\n\n\n<p>This helps you verify that the main web server process (e.g., <code>nginx<\/code> or <code>httpd<\/code>) is running as expected.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Monitor Resource Usage for a Specific Process<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker top my_app_container -eo pid,cmd,%cpu,%mem\n<\/code><\/span><\/pre>\n\n\n<p>This shows the <strong>PID, command, CPU usage, and memory usage<\/strong> for processes in <code>my_app_container<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Use <code>docker top<\/code> in a Script<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\ndocker top my_app_container &gt; process_list.txt\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Process list saved to process_list.txt\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This script saves the process list from <code>my_app_container<\/code> to a file.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Check for Zombie Processes<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker top my_container | grep defunct\n<\/code><\/span><\/pre>\n\n\n<p>This checks for <strong>zombie processes<\/strong> (<code>&lt;defunct&gt;<\/code>) inside the container.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. Combine with <code>watch<\/code> for Real-Time Monitoring<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">watch docker top my_container\n<\/code><\/span><\/pre>\n\n\n<p>This updates the process list every 2 seconds, giving you a real-time view of running processes.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. Check Processes Across Multiple Containers<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker ps -q | xargs -n 1 docker top\n<\/code><\/span><\/pre>\n\n\n<p>This lists processes for all running containers.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Use Cases for <code>docker top<\/code><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Monitoring Running Processes<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Verify that the <strong>main application process<\/strong> is running inside the container.<\/li>\n\n\n\n<li>Example: Ensure the <strong>web server (NGINX or Apache)<\/strong> is active.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Debugging and Troubleshooting<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Identify <strong>stuck or zombie processes<\/strong>.<\/li>\n\n\n\n<li>Debug performance issues by checking which processes consume <strong>high CPU or memory<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Security Auditing<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Monitor processes to detect <strong>unauthorized or malicious processes<\/strong> inside containers.<\/li>\n\n\n\n<li>Example: Ensure only the intended processes are running in production containers.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Incident Response and Forensics<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>docker top<\/code> during incident response to capture the state of processes in a compromised container.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Performance Analysis<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Identify bottlenecks by monitoring the CPU and memory usage of container processes.<\/li>\n\n\n\n<li>Example: Check if a background task is consuming too many resources.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Process-Based Monitoring and Health Checks<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Integrate <code>docker top<\/code> in monitoring scripts to detect missing or stuck processes.<\/li>\n\n\n\n<li>Example: Automatically restart a container if a key process is missing.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>List of Common <code>docker top<\/code> Commands<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Command<\/strong><\/th><th><strong>Description<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>docker top my_container<\/code><\/td><td>List all processes running in <code>my_container<\/code><\/td><\/tr><tr><td><code>docker top my_container -ef<\/code><\/td><td>Show detailed process information (<code>ps -ef<\/code>)<\/td><\/tr><tr><td><code>docker top my_container aux<\/code><\/td><td>Display process info in <code>ps aux<\/code> format<\/td><\/tr><tr><td>`docker top my_container<\/td><td>grep sleep`<\/td><\/tr><tr><td><code>docker top my_app_container -eo pid,cmd,%cpu,%mem<\/code><\/td><td>Show specific columns (PID, command, CPU, memory)<\/td><\/tr><tr><td>`docker ps -q<\/td><td>xargs -n 1 docker top`<\/td><\/tr><tr><td><code>watch docker top my_container<\/code><\/td><td>Monitor container processes in real time<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices for Using <code>docker top<\/code>:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use <code>docker top<\/code> for regular monitoring<\/strong> to verify critical processes.<\/li>\n\n\n\n<li><strong>Combine with <code>grep<\/code> or <code>watch<\/code><\/strong> for real-time process filtering and monitoring.<\/li>\n\n\n\n<li><strong>Automate health checks<\/strong> based on process availability and resource consumption.<\/li>\n\n\n\n<li><strong>Filter columns (<code>-eo<\/code>)<\/strong> for relevant metrics like CPU, memory, and PID.<\/li>\n\n\n\n<li><strong>Capture process lists<\/strong> during debugging or incident response for later analysis.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Errors and Solutions<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>&#8220;No such container&#8221;<\/strong><br>\u2192 Ensure the container exists and is running. Use <code>docker ps<\/code> to verify.<\/li>\n\n\n\n<li><strong>Processes not shown<\/strong><br>\u2192 The container may have stopped or exited. Use <code>docker ps -a<\/code> to check its status.<\/li>\n\n\n\n<li><strong>Zombie processes detected (<code>&lt;defunct><\/code>)<\/strong><br>\u2192 Investigate the main process or entrypoint script for mismanagement of child processes.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Combining <code>docker top<\/code> with Other Commands<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Check Resource Usage with <code>docker stats<\/code>:<\/strong> <code>docker stats my_container docker top my_container<\/code><\/li>\n\n\n\n<li><strong>Restart Container if a Process is Missing:<\/strong> <code>if ! docker top my_app | grep \"nginx\"; then docker restart my_app fi<\/code><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here\u2019s a complete tutorial on docker top, covering what it does, examples, and use cases. What is docker top? docker top is a Docker command used to display the running&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4862],"tags":[],"class_list":["post-48416","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=48416"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48416\/revisions"}],"predecessor-version":[{"id":48418,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48416\/revisions\/48418"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=48416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=48416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=48416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}