Mate Varga Mate Varga - 6 months ago 28
Linux Question

Determining if a process runs inside lxc/Docker

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?

Thanks.

Answer

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; which, with Docker containers, will be something like /lxc/<containerid>.