Skip to content

Commit

Permalink
Add an id for Docker containers
Browse files Browse the repository at this point in the history
Usual Linux images for Docker containers, does not have the originally listed files.
Following the conversation at denisbrodbeck/machineid#10, I am including the following:
Because `/proc/self/cgroup` seams to not be working on same Docker versions, I am also including `/proc/self/mountinfo`.

Tested on `Python 3.11` running on `Debian GNU/Linux 11 (bullseye)` inside a Docker.
  • Loading branch information
juanbretti authored Jan 27, 2023
1 parent f9a781d commit 14e376b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion machineid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ def id() -> str:
id = __read__('/var/lib/dbus/machine-id')
if not id:
id = __read__('/etc/machine-id')

if not id:
id = __exec__('head -1 /proc/self/cgroup|cut -d/ -f3')
if not id:
id = __exec__("grep 'systemd' /proc/self/mountinfo|cut -d/ -f3")

if platform.startswith('openbsd') or platform.startswith('freebsd'):
id = __read__('/etc/hostid')
if not id:
Expand Down

0 comments on commit 14e376b

Please sign in to comment.