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?
相关问题
- Docker task in Azure devops won't accept "$(pw
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- JQ: Select when attribute value exists in a bash a
- Unable to run mariadb when mount volume
Maybe this do the trick:
Is that what you want? Help it help =)
The easiest way would be to check the environment. If you have the
container=lxc
variable, you are within a container.Otherwise, if you are root, you can try to perform
mknod
ormount
operation, if it fails, you are most likely in a container with dropped capabilities.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 LXC/Docker containers, will be something like/lxc/<containerid>
or/docker/<containerid>
respectively.This SO Q&A: "Find out if the OS is running in a virtual environment"; though not the same as the OP's question, it does indeed answer common cases of finding which container you're in (if at all).
In particular, install and read the code of this bash script which seems to work pretty well:
virt-what :
Handy Python function to check if running in Docker (linux-only, obvs.):
On a new ubuntu 16.04 system, new systemd & lxc 2.0