dgtlmoon / changedetection.io

The best and simplest free open source web page change detection, website watcher, restock monitor and notification service. Restock Monitor, change detection. Designed for simplicity - Simply monitor which websites had a text change for free. Free Open source web page change detection, Website defacement monitoring, Price change notification
https://changedetection.io
Apache License 2.0
16.58k stars 915 forks source link

[feature] DBus notification #2481

Open dgtlmoon opened 1 month ago

dgtlmoon commented 1 month ago

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

Originally posted by **Iey4iej3** July 10, 2024 **Version and OS** 0.45.25 on linux/docker **Is your feature request related to a problem? Please describe.** DBus notification is supported by apprise: https://github.com/caronc/apprise/wiki/Notify_dbus However, if I input `dbus://` in notification, it is not recognized. **Describe the solution you'd like** Support `dbus://` notification **Describe the use-case and give concrete real-world examples** (This is used when the notification is local) **Additional context**
dgtlmoon commented 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:

1. Install D-Bus and Notification Tools in the Docker Container

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"]

2. Write a Notification Script

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"

3. Running the Container with D-Bus Socket

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

4. Handling the DBus Session

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.

5. Example Run Command

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

Summary

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.