2024-07-11
6 min read

ps Command Doesn't Work in Docker Container

ps Command Doesn't Work in Docker Container

TLDR

The ps command might not work in Docker containers due to missing packages or limited process visibility. Install procps or use alternative tools to inspect processes.


The ps command is commonly used to list running processes, but it might not work as expected in Docker containers. This guide explains why and provides solutions to troubleshoot and resolve the issue.

Why Doesn't ps Work?

Docker containers often use minimal base images to reduce size. These images might not include the procps package, which provides the ps command. Additionally, containers have limited visibility into processes outside their namespace.

Step 1: Check for procps

Verify if the procps package is installed in your container.

Run the following command inside your Docker container:

ps

You might see an error like this:

bash: ps: command not found

If you see this error, the procps package is missing.

Step 2: Install procps

Install the procps package to enable the ps command.

Command

For Debian/Ubuntu-based images:

apt-get update && apt-get install -y procps

For Alpine-based images:

apk add procps

Explanation

  • apt-get update: Updates package lists.
  • apt-get install -y procps: Installs procps on Debian/Ubuntu.
  • apk add procps: Installs procps on Alpine.

Step 3: Use Alternative Tools

If installing procps is not an option, use alternative tools to inspect processes.

Example

cat /proc/<pid>/status

Explanation

  • /proc/<pid>/status: Provides detailed information about a specific process.

Step 4: Debugging Process Visibility

Containers have limited visibility into processes outside their namespace. Use the docker top command to list processes running in a container.

Command

docker top <container_name>

Example Output

PID    USER   COMMAND
12345  root   /bin/bash
67890  root   sleep 100

Explanation

  • docker top: Lists processes running inside the container.

Best Practices

  • Use Minimal Images: Only install necessary packages.
  • Automate Setup: Include procps installation in your Dockerfile if ps is required.
  • Understand Namespaces: Learn how Docker isolates processes.

By following these steps, you can resolve issues with the ps command in Docker containers and effectively inspect running processes.

Published: 2024-07-11|Last updated: 2024-07-11T09:00:00Z

Found an issue?