Docker
ps command
troubleshooting
container issues
Linux

ps: command not found in a Docker container (how to install ps)

Codemia Knowledge Hub ·

The ps command fails inside most Docker containers because minimal base images do not include the procps package. Install it with your image's package manager: apt-get install -y procps on Debian or Ubuntu, apk add procps on Alpine. Often you do not need it at all, because docker top, docker stats and /proc give you the same information.

Why ps is missing

Docker images are built to be small. Base images like alpine, distroless, scratch, and the slim variants of debian or ubuntu strip out utilities the application does not strictly need. ps belongs to the procps package (procps-ng on some distributions), which is not considered essential for running a containerized application.

The trade-off is deliberate:

BenefitImpact
Smaller attack surfaceFewer binaries means fewer potential vulnerability vectors
Faster pulls and deploysLess data to transfer over the network
Faster startupLess filesystem overhead during container initialization
Lower storage costsEspecially significant when running hundreds of containers

Installing ps in the Dockerfile

Install procps in the Dockerfile, not interactively in a running container. Interactive installs are lost when the container stops.

Debian / Ubuntu

dockerfile
1FROM ubuntu:22.04
2
3RUN apt-get update && \
4    apt-get install -y --no-install-recommends procps && \
5    rm -rf /var/lib/apt/lists/*
6
7CMD ["bash"]

--no-install-recommends avoids pulling in unnecessary packages, and rm -rf /var/lib/apt/lists/* removes the package index cache so the image stays small.

Alpine

dockerfile
1FROM alpine:3.19
2
3RUN apk add --no-cache procps
4
5CMD ["sh"]

--no-cache avoids storing the package index locally.

Amazon Linux / CentOS / RHEL

dockerfile
1FROM amazonlinux:2023
2
3RUN yum install -y procps-ng && \
4    yum clean all
5
6CMD ["bash"]

On these distributions the package is called procps-ng, not procps. Using the wrong name gives a "package not found" error.

Package names by distribution

Base ImagePackage ManagerPackage NameInstall Command
Ubuntu / Debianaptprocpsapt-get install -y procps
Alpineapkprocpsapk add procps
Amazon Linux / CentOS / RHELyum/dnfprocps-ngyum install -y procps-ng
Fedoradnfprocps-ngdnf install -y procps-ng
Arch Linuxpacmanprocps-ngpacman -S procps-ng

Temporary install in a running container

For a one-off debugging session you can install procps in a running container. The change disappears when the container stops.

bash
1# For Debian/Ubuntu containers
2docker exec -it <container_id> bash -c "apt-get update && apt-get install -y procps"
3
4# For Alpine containers
5docker exec -it <container_id> sh -c "apk add procps"

After that, ps works for the rest of that container's lifetime.

Getting process information without installing anything

Docker can show process information from the host without modifying the container.

docker top

bash
docker top <container_id>

This lists the processes running inside the container, like ps, but runs from the host. You can pass custom ps options:

bash
# With custom ps options
docker top <container_id> -o pid,user,%cpu,%mem,command

docker stats

bash
docker stats <container_id>

This shows real-time CPU, memory, network and I/O statistics for running containers, which is more useful than ps for monitoring resource usage.

Reading /proc directly

The /proc filesystem is always available inside Linux containers, even without procps. For a quick check, reading /proc/1/cmdline or /proc/1/status is faster than installing a package:

bash
1# List all process IDs
2ls /proc/[0-9]*
3
4# Get the command line of PID 1
5cat /proc/1/cmdline | tr '\0' ' '
6
7# Get memory status of PID 1
8cat /proc/1/status | grep -i vmrss
9
10# List all processes with their command names
11for pid in /proc/[0-9]*; do
12    echo "$(basename $pid): $(cat $pid/comm 2>/dev/null)"
13done

nsenter from the host

For containers that do not even have a shell, use nsenter on the host to enter the container's PID namespace and run the host's ps against a private /proc for that namespace:

bash
1# Get the container's PID on the host
2docker inspect --format '{{.State.Pid}}' <container_id>
3
4# Enter the container's PID namespace and run the host's ps
5nsenter -t <container_pid> -p -- unshare --mount --mount-proc ps aux

This is the option for distroless or scratch images where installing packages is not possible.

Debug containers in Kubernetes

In Kubernetes, an ephemeral debug container lets you inspect a pod without changing it:

bash
kubectl debug -it <pod-name> --image=busybox --target=<container-name>

The debug container shares the process namespace of the target container, so you can run diagnostic tools without touching the application image.

Keeping debug tools out of production images

Every extra binary increases the attack surface. If you need ps during development but not in production, use a multi-stage build:

dockerfile
1# Build stage with debugging tools
2FROM python:3.12-slim AS builder
3RUN apt-get update && apt-get install -y procps
4COPY . /app
5WORKDIR /app
6RUN pip install -r requirements.txt
7
8# Production stage without debugging tools
9FROM python:3.12-slim AS production
10COPY --from=builder /app /app
11WORKDIR /app
12CMD ["python", "main.py"]

The production image stays lean while the build stage has the tools you need.

Alternatively, define a separate debug build target:

dockerfile
1FROM python:3.12-slim AS base
2COPY . /app
3WORKDIR /app
4RUN pip install -r requirements.txt
5CMD ["python", "main.py"]
6
7FROM base AS debug
8RUN apt-get update && \
9    apt-get install -y --no-install-recommends procps curl vim && \
10    rm -rf /var/lib/apt/lists/*
bash
1# Build production image
2docker build --target base -t myapp:prod .
3
4# Build debug image
5docker build --target debug -t myapp:debug .

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design