Search code examples
linuxubuntudockerps

how to redirect ps command in linux to look into other folder than /proc


I am using docker containers and I have mounted host /proc to container /host/proc. Now I want ps command inside docker container to look for processes into /host/proc instead of /proc. or how can I right similar utility to ps


Solution

  • the /proc path is hardcoded in the source tree of the /bin/ps binary file. Thus, you need to recompile /bin/ps

    Follow these steps to recompile /bin/ps inside a container that mounts the host /proc and use this new ps to display the process list of the docker host from the container:

    In this example, I do not use /host/proc but /prod, to avoid modifying the path length to the mount point of the procfs filesystem (increasing the path length could trigger runtime errors in some situations). I also use a container based on OpenSUSE Leap 42.1, since you have not described the base image you are using.

    1- on the docker host, first download the /bin/ps sources:

    fenyo@myhost# mkdir /root/git
    fenyo@myhost# cd /root/git
    fenyo@myhost# git clone https://gitlab.com/procps-ng/procps.git
    fenyo@myhost# cd procps
    

    2- replace any occurence of "/proc by "/prod in **/*.c

    3- run your docker container, mounting procfs on /prod in the container

    fenyo@myhost# docker run -v /proc:/prod -v /root/git/procps:/root/git/procps -t -i --rm opensuse:42.1 bash
    

    4- inside the docker container, recompile /bin/ps

    bash-4.2# cd /root/git/procps
    bash-4.2# zypper install -y gettext gettext-tools autoconf libtool pkg-config gcc make
    bash-4.2# ./autogen.sh
    bash-4.2# ./configure --without-ncurses
    bash-4.2# make
    

    Now, you can use /root/git/procps/ps/pscommand as a replacement for /bin/ps, in order to use /prod instead of /proc. With pscommand, you will get a process list from the host, not from the container:

    bash-4.2# ps -auxgww
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.0  0.0  20064  3076 ?        Ss   23:20   0:00 bash
    root      5199  0.0  0.0  33340  2996 ?        R+   23:24   0:00 ps -auxgww
    
    bash-4.2# /root/git/procps/ps/pscommand -auxgww
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.0  0.0  33620  3956 ?        Ss   Jul03   1:13 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
    root         2  0.0  0.0      0     0 ?        S    Jul03   0:01 [kthreadd]
    root         3  0.0  0.0      0     0 ?        R    Jul03   4:44 [ksoftirqd/0]
    root         5  0.0  0.0      0     0 ?        S<   Jul03   0:01 [kworker/0:0H]
    root         7  0.0  0.0      0     0 ?        S    Jul03  15:27 [rcu_preempt]
    root         8  0.0  0.0      0     0 ?        S    Jul03   0:00 [rcu_sched]
    root         9  0.0  0.0      0     0 ?        S    Jul03   0:00 [rcu_bh]
    root        10  0.0  0.0      0     0 ?        S    Jul03  10:02 [rcuop/0]
    root        11  0.0  0.0      0     0 ?        S    Jul03   0:00 [rcuos/0]
    root        12  0.0  0.0      0     0 ?        S    Jul03   0:00 [rcuob/0]
    root        13  0.0  0.0      0     0 ?        S    Jul03   0:06 [migration/0]
    root        14  0.0  0.0      0     0 ?        S    Jul03   0:07 [watchdog/0]
    root        15  0.0  0.0      0     0 ?        S    Jul03   0:06 [watchdog/1]
    root        16  0.0  0.0      0     0 ?        S    Jul03   0:04 [migration/1]
    root        17  0.0  0.0      0     0 ?        S    Jul03   1:07 [ksoftirqd/1]
    root        19  0.0  0.0      0     0 ?        S<   Jul03   0:00 [kworker/1:0H]
    root        20  0.0  0.0      0     0 ?        S    Jul03   2:42 [rcuop/1]
    root        21  0.0  0.0      0     0 ?        S    Jul03   0:00 [rcuos/1]
    root        22  0.0  0.0      0     0 ?        S    Jul03   0:00 [rcuob/1]
    root        23  0.0  0.0      0     0 ?        S    Jul03   0:05 [watchdog/2]
    root        24  0.0  0.0      0     0 ?        S    Jul03   0:06 [migration/2]
    root        25  0.0  0.0      0     0 ?        S    Jul03   0:48 [ksoftirqd/2]
    root        27  0.0  0.0      0     0 ?        S<   Jul03   0:00 [kworker/2:0H]
    root        28  0.0  0.0      0     0 ?        S    Jul03   7:32 [rcuop/2]
    root        29  0.0  0.0      0     0 ?        S    Jul03   0:00 [rcuos/2]
    root        30  0.0  0.0      0     0 ?        S    Jul03   0:00 [rcuob/2]
    root        31  0.0  0.0      0     0 ?        S    Jul03   0:05 [watchdog/3]
    root        32  0.0  0.0      0     0 ?        S    Jul03   0:05 [migration/3]
    root        33  0.0  0.0      0     0 ?        S    Jul03   0:37 [ksoftirqd/3]
    [...]