How to determine if a process runs inside lxc/Docker?

后端 未结 16 1384
情歌与酒
情歌与酒 2020-11-28 18:15

Is there any way to determine if a process (script) runs inside an lxc container (~ Docker runtime)? I know that some programs are able to detect whether they run inside a v

16条回答
  •  清歌不尽
    2020-11-28 19:11

    We use the proc's sched (/proc/$PID/sched) to extract the PID of the process. The process's PID inside the container will differ then it's PID on the host (a non-container system).

    For example, the output of /proc/1/sched on a container will return:

    root@33044d65037c:~# cat /proc/1/sched | head -n 1
    bash (5276, #threads: 1)
    

    While on a non-container host:

    $ cat /proc/1/sched  | head -n 1
    init (1, #threads: 1)
    

    This helps to differentiate if you are in a container or not.

提交回复
热议问题