Docker Community Forums

Share and learn in the Docker community.

How can a process detect if it is running in a container?


(Dhruvset) #1

Anything better than this:

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.