546

I want to do a ps command in a docker container derived from Debian official Docker hub repository:

$ docker run -ti debian:wheezy /bin/bash
root@51afd6b09af8:/# ps
bash: ps: command not found
Cœur
  • 37,241
  • 25
  • 195
  • 267
Yves Nicolas
  • 6,901
  • 7
  • 25
  • 40

7 Answers7

923

ps is not installed in the base wheezy image. Try this from within the container:

apt-get update && apt-get install procps

or add the following line to the Dockerfile:

RUN apt-get update && apt-get install -y procps && rm -rf /var/lib/apt/lists/*
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
user2105103
  • 11,599
  • 2
  • 18
  • 11
  • 14
    If you get this error "The command '/bin/sh -c apt-get install procps' returned a non-zero code: 1" then use `apt-get install -y procps` instead. – fred271828 Aug 25 '17 at 17:25
  • 2
    Here is the URL that discussed in 2014 of not including `ps` in wheezy image: `https://github.com/moby/moby/issues/447#issuecomment-36647268` – daparic Jan 29 '19 at 05:11
  • 1
    RUN apt-get update && apt-get install -y procps && rm -rf /var/lib/apt/lists/* – Zhuo YING Nov 25 '19 at 07:51
  • Where should it be added? I think in .yml file when creating your docker image? – Yaro May 28 '20 at 10:46
  • 3
    Remove RUN from your command, I think it is a typo. – tjurkan Oct 01 '20 at 07:40
  • Note that `ps` on the docker host might work fine for you. – mlissner Nov 07 '20 at 16:21
  • @tjurkan I suspect it is not a typo. The line is meant to be added to the Dockerfile that is used to build the container image for the container in question. – Manfred Nov 20 '20 at 01:42
  • Yes, now it is clear, if you use it in the Dockerfile then you need RUN in front of the command, but in case you log to the container with something like: docker exec -it bash, and then execute it, then you don't need the RUN command in front of it. – tjurkan Nov 20 '20 at 08:17
  • After further checking, the command mentioned in the question would download debian:wheezy image from remote repo if it is not present locally and login to the container based on that image as root. From there, RUN command is not necessary in front of other commands. – tjurkan Nov 20 '20 at 11:35
  • The edit is wrong, and I'm going to revert it back: OP is clearly asking _how do I run a ps command in a docker container_ *not* _how do I add ps to the original Dockerfile_, and get it forever into the image. – nnsense Jul 29 '21 at 10:59
  • 3
    My edit has been rejected, apparently. I don't know why, but running `RUN apt-get update && apt-get install -y procps` within a running container will not lead to nothing else than an error. The original post, which was also the accepted answer, was fine as it was. – nnsense Aug 03 '21 at 10:33
  • Yo, get rid of && rm -rf /var/lib/apt/lists/* Its unnecessary and could have unwanted effects on the package manager – Shayne Mar 14 '22 at 06:50
  • It has the *wanted* effect of removing the data that was added by `apt-get update` earlier in the command, to reduce the size of the resulting image. If you're going to add more packages later, you have to run `apt-get update` again anyway. You can't rely on it being done by an earlier layer, since that layer might be in the build cache and have outdated information – PJWeisberg Jul 29 '22 at 18:09
  • This answer helped for LXC container as well. – baptx Jan 06 '23 at 01:32
143

use docker top

docker top <container ID>
es cologne
  • 3,318
  • 2
  • 17
  • 11
93

In case you can't install the procps package (don't have proper permissions) you can use /proc directory.

The first few directories (named as numbers) are PIDs of your processes. Inside directories, you can find additional information useful to decipher which process is connected to each PID. For example, you can use the cat command to view "cmdline" file to check which process is connected to PID.

$ ls /proc
1 10 11 ...

$ ls -1 /proc/22
attr
autogroup
auxv
cgroup
clear_refs
cmdline
...

$ cat /proc/22/cmdline 
/bin/sh

Edited - spaces are lost in the cmdline so we can pipe the cat output to the tr command, for example:

$ cat /proc/1/cmdline | tr '\0' ' '
/sbin/init splash
Vedran Vidovic
  • 1,133
  • 6
  • 6
23

If you're running a CentOS container, you can install ps using this command:

yum install -y procps

Running this command on Dockerfile:

RUN yum install -y procps
Duff Ganther
  • 391
  • 2
  • 6
18

Firstly, run the command below:

apt-get update && apt-get install procps

and then run:

ps -ef
jchanger
  • 739
  • 10
  • 29
harun ugur
  • 1,718
  • 18
  • 18
4

Kali users fix for bash: ps: command not found issue is:

apt install procps
storenth
  • 967
  • 11
  • 18
1

For RockyLinux 9, you need to use this command

$> yum install procps-ng
Skull
  • 119
  • 5