Comments (2)
I would suggest that we use cgroup to determine that we run under docker and than use the hash like string in cgroup as a salt.
Unique literal for docker container: head -1 /proc/self/cgroup|cut -d/ -f3
Usage of Docker: grep -q docker /proc/self/cgroup && echo Docker || echo NO_DOCKER
/proc/self/cgroup
PS H:\> docker run --rm centos:8 /bin/sh -c "cat /proc/self/cgroup"
14:name=systemd:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
13:rdma:/
12:pids:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
11:hugetlb:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
10:net_prio:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
9:perf_event:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
8:net_cls:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
7:freezer:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
6:devices:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
5:memory:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
4:blkio:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
3:cpuacct:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
2:cpu:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
1:cpuset:/docker/4531c6cdf6e13484be06e3615ebf4721c51a0b814555b15c210115762fc5b484
Try different docker images
docker run --rm ubuntu:16.04 /bin/sh -c "head -1 /proc/self/cgroup|cut -d/ -f3"
docker run --rm ubuntu:21.04 /bin/sh -c "head -1 /proc/self/cgroup|cut -d/ -f3"
docker run --rm alpine:3.13.2 /bin/sh -c "head -1 /proc/self/cgroup|cut -d/ -f3"
docker run --rm centos:6 /bin/sh -c "head -1 /proc/self/cgroup|cut -d/ -f3"
docker run --rm centos:7 /bin/sh -c "head -1 /proc/self/cgroup|cut -d/ -f3"
docker run --rm centos:8 /bin/sh -c "head -1 /proc/self/cgroup|cut -d/ -f3"
Output:
2996761066b129c02629c0a96d56cb1a87ff07df43b2ce67085e4010ce384c68
d70a6db52902f2cdbed846644beb78ec2d3c10331157ef6caf4a78c74dd60e88
0ce0ba2f7975cd95f9bb9d72929509f5c5c12c6b87ea53a3aa02de2e30001839
745a00b110b7c73656a1ada91351338f1587dde0a39e264143d3b25994f87816
fc92e7ad9102150d1f1314d0b78523054e7ef0115ecf120a2db5ad1b792a6227
6b28b03a20d07c19e2543c6d658721226ffb6ccbccbd162c144377874332bf2e
from machineid.
Looks like the /proc/self/cgroup
works in most cases.
But there are some where could return nothing.
Take a look at:
- https://stackoverflow.com/questions/69002675/on-debian-11-bullseye-proc-self-cgroup-inside-a-docker-container-does-not-sho
- https://stackoverflow.com/questions/68816329/how-to-get-docker-container-id-from-within-the-container-with-cgroup-v2
Another idea is to extract the system
from /proc/self/mountinfo
.
Here is a PR with this proposal: keygen-sh/py-machineid#3
from machineid.
Related Issues (11)
- FreeNAS (FreeBSD variant) doesn't display any content with smbios.system.uuid property HOT 2
- OS centos6 error
- Supporting an alternate source for machine-id for testing purposes? HOT 1
- [question] using machineid on docker container. HOT 1
- ESXi host support HOT 1
- Error while getting machine id in Windows 10 HOT 9
- Docker machine-id fails
- No machine-id detected in docker containers HOT 6
- Why different machines has same machineId? HOT 2
- OpenBSD uses machine-id HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from machineid.