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
Docker is evolving day by day, so we can't say for sure if they are going to keep
.dockerenv .dockerinit
in the future.In most of the Linux flavours
init
is the first process to start. But in case of containers this is not true.Check for all the solutions above in Python:
Docker creates a
.dockerenv
file at the root of the directory tree inside container. You can run this script to verifyMORE: Ubuntu actually has a bash script:
/bin/running-in-container
and it actually can return the type of container it has been invoked in. Might be helpful. Don't know about other major distros though.Building on the accepted answer that tests
/proc/*/cgroup
..So for use in a script or so, a test could be constructed this way.
A concise way to check for docker in a bash script is:
My answer only applies for Node.js processes but may be relevant for some visitors who stumble to this question looking for a Node.js specific answer.
I had the same problem and relying on
/proc/self/cgroup
I created an npm package for solely this purpose — to detect whether a Node.js process runs inside a Docker container or not.The containerized npm module will help you out in Node.js. It is not currently tested in Io.js but may just as well work there too.