Difficulty: Beginner
How do you see the processes running inside a container, and how is docker top different from running ps inside it?
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.
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:
ps.strace, kill or nsenter from the host./etc/passwd. A process running as container UID 1000 may show the host user who happens to own UID 1000, or just the number.| Command | What you get | Needs tools in the image |
|---|---|---|
docker top web | Process list with host PIDs | No |
docker exec web ps aux | Process list with namespace PIDs (main process is 1) | Yes, ps (procps or busybox) |
docker stats web | Live CPU, memory, network, block I/O and PID count per container | No |
docker inspect -f '{{.State.Pid}}' web | Host PID of the container's main process | No |
/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.Z): a PID 1 that does not reap children, common when an app spawns subprocesses. Running with --init fixes it.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.
# 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.
# 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.
docker top, PID namespace, docker exec ps, docker stats, Process inspection