Open dgtlmoon opened 1 month ago
Running D-Bus and interacting with it from inside a Docker container, particularly for tasks like sending desktop notifications, requires some specific setup. Here is a step-by-step guide to achieve this:
First, make sure your Dockerfile includes the necessary packages for D-Bus and notifications. Here's an example Dockerfile for an Ubuntu-based image:
FROM ubuntu:20.04
# Install necessary packages
RUN apt-get update && apt-get install -y \
dbus \
dbus-x11 \
libnotify-bin \
&& rm -rf /var/lib/apt/lists/*
# Add a user for non-root operation
RUN useradd -ms /bin/bash dockeruser
# Switch to the created user
USER dockeruser
WORKDIR /home/dockeruser
# Copy the script to the container
COPY notify.sh /home/dockeruser/notify.sh
RUN chmod +x /home/dockeruser/notify.sh
# Entry point
ENTRYPOINT ["/home/dockeruser/notify.sh"]
Create a simple shell script (e.g., notify.sh
) to send notifications:
#!/bin/bash
# Send a test notification
notify-send "Hello from Docker" "This is a test notification"
When running the container, you'll need to share the host's D-Bus socket with the container. You can achieve this by mounting the socket as a volume. Here's how to do it:
docker build -t dbus-notify .
docker run -it --rm \
--name dbus-notify \
-v /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket \
-e DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus" \
dbus-notify
To handle the D-Bus session, you need to ensure that the container has access to the correct D-Bus session address. This typically involves exporting the DBUS_SESSION_BUS_ADDRESS
environment variable correctly.
For the notification to appear on the host system, you must pass the D-Bus session bus address. Here’s an example of how to do this in a more complete manner:
# Find the D-Bus session bus address
DBUS_SESSION_BUS_ADDRESS=$(dbus-launch | grep -E -o 'unix:path=/run/user/[0-9]+/bus')
# Run the container with the DBus session environment variable
docker run -it --rm \
-v /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket \
-v /run/user/$(id -u)/bus:/run/user/$(id -u)/bus \
-e DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
dbus-notify
By following these steps, you should be able to run a Docker container that can send desktop notifications using D-Bus. The key points are installing the necessary packages, mounting the D-Bus socket, and correctly setting the DBUS_SESSION_BUS_ADDRESS
environment variable. This setup allows the containerized application to communicate with the host's D-Bus system and send notifications to the desktop.
So the actual container does not have dbus:// support, which needs a few library from apt-get added, tho it depends of rPi etc etc can also package/build with it.
Discussed in https://github.com/dgtlmoon/changedetection.io/discussions/2480