Skip to content

Commit

Permalink
add docker support
Browse files Browse the repository at this point in the history
* Add an id for Docker containers

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.

* Included "if 'docker' in mountinfo" for "linux"

Following your review, I made some updates on the patch.

* Update __init__.py

* Fixed the indentation to 2.
* Checked on a Docker container the `if 'docker' in cgroup:` works when the `docker` is not found. Same test with `mountinfo`.
* When the full code does not find an ID raises the following:

`Python 3.11.0 (main, Nov 15 2022, 19:58:01) [GCC 10.2.1 20210110] on linux                                                    
Type "help", "copyright", "credits" or "license" for more information.                                                        
>>> import machineid                                                                                                          
>>> print(machineid.id())                                                                                                     
Traceback (most recent call last):                                                                                            
  File "<stdin>", line 1, in <module>                                                                                         
  File "/usr/local/lib/python3.11/site-packages/machineid/__init__.py", line 90, in id                                        
    raise Exception('failed to obtain id on platform {}'.format(platform))                                                    
Exception: failed to obtain id on platform linux`

* Update __init__.py

Check if `cgroup` and `mountinfo` are not None, before checking if `docker` is inside the file.
To not rise a possible error when is trying to check `if 'docker' in None` (when the file does not exist).
  • Loading branch information
juanbretti authored Feb 1, 2023
1 parent f9a781d commit 7869558
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions machineid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def id() -> str:
id = __read__('/var/lib/dbus/machine-id')
if not id:
id = __read__('/etc/machine-id')
if not id:
cgroup = __read__('/proc/self/cgroup')
if cgroup:
if 'docker' in cgroup:
id = __exec__('head -1 /proc/self/cgroup | cut -d/ -f3')
if not id:
mountinfo = __read__('/proc/self/mountinfo')
if mountinfo:
if 'docker' in mountinfo:
id = __exec__("grep 'systemd' /proc/self/mountinfo | cut -d/ -f3")

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

0 comments on commit 7869558

Please sign in to comment.