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: Installsprocpson Debian/Ubuntu.apk add procps: Installsprocpson 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
procpsinstallation in your Dockerfile ifpsis 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.
Related Resources
- Docker Ubuntu: Ping Not Found — install missing tools
- Docker Alpine: How to Use Bash — shell tools in Alpine
- How to Access Docker Container Shell — shell access
- Introduction to Docker Guide — Docker fundamentals
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
Docker: Name is Already in Use by Container
Encountering the 'name is already in use by container' error in Docker? Learn why it happens and how to resolve it effectively.
Docker Multi-Stage Build Optimization
Learn to create efficient Docker images using multi-stage builds to reduce image size and improve security.
60 minutes
Docker Security Hardening Checklist
Comprehensive security checklist for hardening Docker containers, images, and runtime environments.
60-90 minutes