0

I am building a Docker image for running a bunch of background processes that communicate with each other using D-Bus. I am basing the image on Ubuntu Bionic. The system already works on native Debian 9.

What do I need to do to make D-Bus work inside the container? I have found some ideas by googling, but most questions on the internet are about using D-Bus between the container and the host, while I only need to connect processes running inside the container. My most successful attempts so far have failed with error message suggesting that there is something missing related to X11, which is true, because at this point, I don't have X11 installed at all.

An example of D-Bus service that I would run and connect to inside the container:

#!/usr/bin/env python

import gobject
import dbus
import dbus.service
import dbus.mainloop.glib
import sys

service_name = "com.testservice"
object_path = "/testservice"
interface_name = service_name

class SomeObject(dbus.service.Object):

    def __init__(self, bus, object_path):
        dbus.service.Object.__init__(self, bus, object_path)
    
    @dbus.service.method(service_name, in_signature='', out_signature='')
    def Exit(self):
        mainloop.quit()


if __name__ == '__main__':
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    session_bus = dbus.SessionBus()
    name = dbus.service.BusName(service_name, session_bus)
    object = SomeObject(session_bus, object_path)

    mainloop = gobject.MainLoop()
    mainloop.run()

Currently, attempting to start that service gives this output:

Traceback (most recent call last):
  File "/hostwork/dbus-test.py", line 35, in <module>
    session_bus = dbus.SessionBus()
  File "/usr/lib/python2.7/dist-packages/dbus/_dbus.py", line 211, in __new__
    mainloop=mainloop)
  File "/usr/lib/python2.7/dist-packages/dbus/_dbus.py", line 100, in __new__
    bus = BusConnection.__new__(subclass, bus_type, mainloop=mainloop)
  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 122, in __new__
    bus = cls._new_for_bus(address_or_type, mainloop=mainloop)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
1

I found a solution myself:

In my Dockerfile, I install package dbus but nothing related to X11. I also install all dependencies of my software, including those of the Python script I shared in the question.

In my Dockerfile, I have these lines:

COPY start.sh /usr/local/bin
RUN chmod +x /usr/local/bin/start.sh
ENTRYPOINT ["/bin/bash", "/usr/local/bin/start.sh"]

Content of start.sh is this:

#!/usr/bin/env bash

export DBUS_SESSION_BUS_ADDRESS=`dbus-daemon --fork --config-file=/usr/share/dbus-1/session.conf --print-address`

exec bash

This way, running the container gives me a shell with DBUS_SESSION_BUS_ADDRESS set appropriately. I can run the test script background and say

dbus-send --print-reply --session --dest=com.testservice /testservice com.testservice.Exit

to kill it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.