Docker Top

Difficulty: Beginner

Question

How do you see the processes running inside a container, and how is docker top different from running ps inside it?

Answer

Run docker top <container>. It lists the processes running in that container by querying from the host side, so it works even if the image has no ps binary, and it accepts ps options such as docker top web -eo pid,ppid,user,%cpu,%mem,cmd. The main difference from docker exec <container> ps is the view: docker top shows host PIDs, while ps inside the container shows PIDs from the container's own PID namespace, where the main process is PID 1.

How docker top works

Containers use Linux namespaces, and the container's processes are ordinary processes on the host. Docker knows which processes belong to which container (through the container's cgroup) and runs ps on the host, filtered to those PIDs. That has a few practical consequences:

  • It works on minimal, distroless and scratch images with no shell or ps.
  • PIDs are host PIDs, which is what you need if you want to strace, kill or nsenter from the host.
  • Usernames are resolved against the host's /etc/passwd. A process running as container UID 1000 may show the host user who happens to own UID 1000, or just the number.
  • The container must be running; a stopped container has no processes.

Comparing the options

CommandWhat you getNeeds tools in the image
docker top webProcess list with host PIDsNo
docker exec web ps auxProcess list with namespace PIDs (main process is 1)Yes, ps (procps or busybox)
docker stats webLive CPU, memory, network, block I/O and PID count per containerNo
docker inspect -f '{{.State.Pid}}' webHost PID of the container's main processNo

What to look for

  • Is the right thing PID 1? If you see /bin/sh -c node server.js as the top process, the image uses shell-form CMD and your app will not receive SIGTERM on stop.
  • Zombie processes (state Z): a PID 1 that does not reap children, common when an app spawns subprocesses. Running with --init fixes it.
  • Unexpected processes: something like a crypto miner in a public-facing container is a classic sign of compromise.
  • Too many workers: Gunicorn or PM2 starting one worker per host CPU instead of respecting the container's CPU limit, which causes memory pressure.

For resource trends, use docker stats (or docker stats --no-stream for a single snapshot). For deep debugging, get the host PID from docker inspect and use host tools such as nsenter -t <pid> -n to enter just the container's network namespace.

Code examples

docker top with custom ps columns

# Default view
docker top orders-api

# Pick the columns you care about (ps -o syntax)
docker top orders-api -eo pid,ppid,user,stat,%cpu,%mem,cmd

docker top passes the options to ps on the host, so you get host PIDs and only this container's processes.

Namespace view versus host view

# Inside the container: the app is PID 1
docker exec orders-api ps -o pid,comm

# Host PID of that same main process
docker inspect -f '{{.State.Pid}}' orders-api

# Live resource usage snapshot
docker stats --no-stream orders-api

The same process is PID 1 in the container and 48213 on the host, which is why the two commands show different numbers.

Key points

  • docker top lists a running container's processes from the host side and accepts ps options.
  • It shows host PIDs; docker exec ps shows namespace PIDs where the main process is 1.
  • It works on images with no shell or ps, including distroless.
  • Use docker stats for live CPU and memory, docker top for the process list.
  • Check that your app, not a shell, is PID 1, and look for zombie processes.

Common follow-up questions

  • Why does the main process show as PID 1 inside the container?
  • How would you strace a process running inside a container?
  • What causes zombie processes in containers and how do you prevent them?
  • How do you limit the number of processes a container can create?

Interview tips

  • Mention the host PID versus namespace PID difference; it shows you understand namespaces.
  • Use the question to bring up the PID 1 and signal handling point.
  • Pair docker top with docker stats so the interviewer sees you know which tool answers which question.

Concepts covered

docker top, PID namespace, docker exec ps, docker stats, Process inspection

More Docker & Containers questions