Coder Social home page Coder Social logo

Comments (6)

cecchisandrone avatar cecchisandrone commented on August 18, 2024 13

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

from machineid.

illuspas avatar illuspas commented on August 18, 2024 5

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

from machineid.

panta avatar panta commented on August 18, 2024 5

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!

from machineid.

andig avatar andig commented on August 18, 2024 2

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

from machineid.

discobean avatar discobean commented on August 18, 2024

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.

from machineid.

uudashr avatar uudashr commented on August 18, 2024

Is there any progress on this?

from machineid.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.