Hi there @ajlennon,
You should be able to publish container hostnames from a service container on a device with the device’s IP address by using the DBus interface to the Avahi service running on the host.
If you’re running singlecontainer, then this will be privileged and you won’t need to do anything extra apart from installing the DBus library via your Dockerfile. If you’re using multicontainer, you’ll need the io.balena.features.dbus
label set. You’ll also need to set the DBus socket address. There’s more information on working with DBus in balena here.
Briefly, using a Debian base image, you should be able to:
RUN apt-get update && \
apt-get install -yq --no-install-recommends \
libdbus-glib-1-dev \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
in your Dockerfile to install the DBus library, and then use it in your application to call Avahi via it. In the following snippet, I’m using NodeJS (via TypeScript) and using the dbus-native
NPM module (I also use Bluebird here for async callbacks):
import { Message, systemBus } from 'dbus-native';
import * as Bluebird from 'bluebird';
...
const dbus = systemBus();
const dbusInvoker = (message: Message): PromiseLike<any> => {
return Bluebird.fromCallback(cb => {
return dbus.invoke(message, cb);
});
};
...
const group = await dbusInvoker({
destination: 'org.freedesktop.Avahi',
path: '/',
interface: 'org.freedesktop.Avahi.Server',
member: 'EntryGroupNew',
});
await dbusInvoker({
destination: 'org.freedesktop.Avahi',
path: group,
interface: 'org.freedesktop.Avahi.EntryGroup',
member: 'AddAddress',
body: [-1, -1, 0x10, 'myNewHostname', '192.168.1.20'],
signature: 'iiuss',
});
await dbusInvoker({
destination: 'org.freedesktop.Avahi',
path: group,
interface: 'org.freedesktop.Avahi.EntryGroup',
member: 'Commit',
});
You should be able to replace myHostname
with the container hostname programmatically however you choose, (eg. via require('os').hostname()
for NodeJS), and the IP address for the host can easily be achieved by querying the Supervisor API (the device endpoint, here has an ip_address
field with the current IP facing IP addresses).
Hopefully this is enough to get you going, let us know if anything’s unclear!