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

No machine-id detected in docker containers #5

Open
lox opened this issue Aug 1, 2018 · 6 comments
Open

No machine-id detected in docker containers #5

lox opened this issue Aug 1, 2018 · 6 comments

Comments

@lox
Copy link
Contributor

lox commented Aug 1, 2018

It appears that Docker doesn't expose /var/lib/dbus/machine-id or /etc/machine-id, so no machine-id gets detected inside docker containers.

@cecchisandrone
Copy link

cecchisandrone commented Aug 10, 2018

Did you try to share the file from the host to the container?

For example:

version: '3'
services:
  service:
    image: image
    ports:
    - 8080:8080
    restart: always
    network_mode: "host"
    volumes:
    - /etc/machine-id:/etc/machine-id
    - /var/lib/dbus/machine-id:/var/lib/dbus/machine-id

@discobean
Copy link

This is a great library, thanks so much for writing it.

It would be great if docker containerisation were detected, and then a unique container ID or some information was used as the machine ID - or if it exposed a Docker ID and the machine ID, and both could be used as a third unique ID.

@illuspas
Copy link

If neither of these paths exists, an empty string will be returned.
Instead of getting a wrong id, it's better to generate one.

// +build linux

package machineid

const (
	// dbusPath is the default path for dbus machine id.
	dbusPath = "/var/lib/dbus/machine-id"
	// dbusPathEtc is the default path for dbus machine id located in /etc.
	// Some systems (like Fedora 20) only know this path.
	// Sometimes it's the other way round.
	dbusPathEtc = "/etc/machine-id"

	uuid = "/proc/sys/kernel/random/uuid"
)

// machineID returns the uuid specified at `/var/lib/dbus/machine-id` or `/etc/machine-id`.
// If there is an error reading the files an empty string is returned.
// See https://unix.stackexchange.com/questions/144812/generate-consistent-machine-unique-id
func machineID() (string, error) {
	id, err := readFile(dbusPath)
	if err != nil {
		// try fallback path
		id, err = readFile(dbusPathEtc)
	}
	
	if err != nil {
		id, err = readFile(uuid)
		if err == nil {
			writeFile(dbusPathEtc, id)
		}
	}

	if err != nil {
		return "", err
	}
	return trim(string(id)), nil
}

func writeFile(filename string, data []byte) error {
	return ioutil.WriteFile(filename, data, 644)
}

It works fine for my docker image

@andig
Copy link

andig commented Feb 16, 2020

Joining the party. It would be great to support docker by getting unique ids per container.

@uudashr
Copy link

uudashr commented Mar 18, 2020

Is there any progress on this?

panta added a commit to panta/machineid that referenced this issue Dec 30, 2020
The machine id is looked in "canonical" locations. If not value is found, one is generated and persisted.
The machine id is looked in:
  - the file pointed by the `MACHINE_ID_FILE` env var
  - `/var/lib/dbus/machine-id`
  - `/etc/machine-id`
  - `$HOME/.config/machine-id`

If no such file is found, a random uuid is generated and persisted in the first
writable file among `$MACHINE_ID_FILE`, `/var/lib/dbus/machine-id`, `/etc/machine-id`, `$HOME/.config/machine-id`.

If there is an error reading _all_ the files an empty string is returned.

The logic implemented is a variation of the one described in denisbrodbeck#5 (comment)
@panta
Copy link

panta commented Dec 30, 2020

If neither of these paths exists, an empty string will be returned.
Instead of getting a wrong id, it's better to generate one.

Since this repository seems unmaintained, I implemented a variation of this logic in github.com/panta/machineid, where it's also possible to specify the machine-id file using the MACHINE_ID_FILE env var.
Obviously I'd be more that happy if the changes were merged upstream!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants