Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an id for Docker containers #3

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
juanbretti marked this conversation as resolved.
Show resolved Hide resolved
if not id:
id = __exec__("grep 'systemd' /proc/self/mountinfo|cut -d/ -f3")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
id = __exec__("grep 'systemd' /proc/self/mountinfo|cut -d/ -f3")
id = __exec__("grep 'systemd' /proc/self/mountinfo | cut -d/ -f3")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give me more detail on what /proc/self/mountinfo is and how it can be used as a unique ID? I don't think mountinfo is Docker-specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezekg , this idea of using mountinfo, came from this post: https://stackoverflow.com/a/71823877/3780957
This post mentions, some versions do not have the ID at /proc/self/cgroup.
I am not sure if this is Docker specific. I can confirm that I check on a Docker instance, and that file has the same ID as /proc/self/cgroup.

Copy link
Member

@ezekg ezekg Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got more info here. I think this is fine as long as you add a similar if 'docker' in mountinfo check as above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also more discussion with the above workarounds is at opencontainers/runtime-spec#1105.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question, if __read__('/proc/self/mountinfo') returns None (because the file does not exist).
Will if 'docker' in mountinfo: return an exception?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right. I was only supplying pseudo code. I guess we should also check if mountinfo and 'docker' in mountinfo:. We shouldn’t raise an exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to fix the indentation (you're using 4 spaces but the rest of the file uses 2 spaces).

Please make sure the code runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right. I was only supplying pseudo code. I guess we should also check if mountinfo and 'docker' in mountinfo:. We shouldn’t raise an exception.

There is no additional exception.

Currently on a Docker container the if 'docker' in cgroup: does not find docker, the ending Exception works as intended. 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


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