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

Mate Varga picture Mate Varga · Nov 15, 2013 · Viewed 67.6k times · Source

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 virtual machine, is something similar available for lxc/docker?

Answer

jpetazzo picture jpetazzo · Nov 16, 2013

The most reliable way is to check /proc/1/cgroup. It will tell you the control groups of the init process, and when you are not in a container, that will be / for all hierarchies. When you are inside a container, you will see the name of the anchor point. With LXC/Docker containers, it will be something like /lxc/<containerid> or /docker/<containerid> respectively.