程式扎記: [ 常見問題 ] Determining if a process runs inside lxc/Docker

標籤

2015年9月20日 星期日

[ 常見問題 ] Determining if a process runs inside lxc/Docker

Source From Here 
Question 
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? 

How-To 
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/. For example: 
# cat /proc/1/cgroup | grep cpu: // In Host
3:cpu:/
# docker ps -q // We have one container running with below id
e1860d7986dd
# docker exec e1860d7986dd cat /proc/1/cgroup | grep cpu:
3:cpu:/docker/e1860d7986dd5116fc18bc71e424b566a64b727d340ece316e128ce59e44d103

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!