CLOVER🍀

That was when it all began.

Dockerコンテナー内で実行したnotify-sendコマンドから、ホスト側のUbuntu Linux 24.04 LTSにデスクトップ通知を行う

これは、なにをしたくて書いたもの?

notify-sendコマンドを使って、デスクトップ通知を行う方法を調べました。

Ubuntu Linux 24.04 LTSでデスクトップ通知をする(notify-sendコマンド) - CLOVER🍀

今回はDockerコンテナー内からホスト側のデスクトップ環境に通知をする方法を調べてみます。

デスクトップ通知とD-Bus

デスクトップ通知にはD-Busというメッセージバスシステムを使っています。

Basic Design | Desktop Notifications Specification

dbus

またD-Busにはシステム全体のものとユーザーログインセッションごとの2つのDaemonがあるという話でした。

今回はこのユーザーログインセッションごとのDaemonを使います。このアドレスはDBUS_SESSION_BUS_ADDRESS
設定されているようです。

The address of the login session message bus is given in the DBUS_SESSION_BUS_ADDRESS environment variable. If that variable is not set, applications may also try to read the address from the X Window System root window property _DBUS_SESSION_BUS_ADDRESS. The root window property must have type STRING. The environment variable should have precedence over the root window property.

D-Bus Specification

こちらを使って、Dockerコンテナー内からホスト側のデスクトップ環境に通知を行ってみます。

環境

今回の環境はこちら。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.4 LTS
Release:        24.04
Codename:       noble


$ uname -srvmpio
Linux 6.8.0-107-generic #107-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 13 19:51:50 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux

Docker。

$ docker version
Client: Docker Engine - Community
 Version:           29.3.1
 API version:       1.54
 Go version:        go1.25.8
 Git commit:        c2be9cc
 Built:             Wed Mar 25 16:13:43 2026
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          29.3.1
  API version:      1.54 (minimum version 1.40)
  Go version:       go1.25.8
  Git commit:       f78c987
  Built:            Wed Mar 25 16:13:43 2026
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v2.2.2
  GitCommit:        301b2dac98f15c27117da5c8af12118a041a31d9
 runc:
  Version:          1.3.4
  GitCommit:        v1.3.4-0-gd6d73eb8
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Dockerコンテナー内からホスト側のデスクトップ環境に通知を送る

現在のユーザーIDを確認してみます。

$ id -u
1000

環境変数DBUS_SESSION_BUS_ADDRESSの値は以下になりました。

$ printenv DBUS_SESSION_BUS_ADDRESS
unix:path=/run/user/1000/bus

UNIXドメインソケットですね。こちらをコンテナーからマウントして使えばよさそうです。

なのですが、いろいろあった結果(後述)Dockerfileまで書くことにしました。

Dockerfile

FROM ubuntu:24.04

RUN apt update \
    && apt install -y --no-install-recommends libnotify-bin \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

Dockerイメージをビルド。

$ docker image build -t kazuhira/notify:latest .

こんな指定で実行。コンテナー内のユーザーIDはホスト側と同じにして、DBUS_SESSION_BUS_ADDRESS環境変数と
マウントするUNIXドメインソケット(/run/user/$(id -u)/bus)もホスト側と同じ値にしました。さらにAppArmorを無効に
します。

$ docker container run -it --rm -u $(id -u) \
    -e DBUS_SESSION_BUS_ADDRESS=$(printenv DBUS_SESSION_BUS_ADDRESS) \
    -v /run/user/$(id -u)/bus:/run/user/$(id -u)/bus \
    --security-opt apparmor=unconfined \
    kazuhira/notify:latest bash

通知を送ってみます。

$ notify-send 'title' 'message'

成功です。

--security-opt apparmor=unconfinedオプションがないと、AppArmorに止められます。

GDBus.Error:org.freedesktop.DBus.Error.AccessDenied: An AppArmor policy prevents this sender from sending this message to this recipient; type="method_call", sender="(null)" (inactive) interface="org.freedesktop.DBus" member="Hello" error name="(unset)" requested_reply="0" destination="org.freedesktop.DBus" (bus)

AppArmor security profiles for Docker | Docker Docs

AppArmorを完全に無効化していますが、これは自己責任で。

また、ホスト側と異なるユーザーIDを使用するとこれもダメなようです。

たとえばrootユーザーは0ですが

$ docker container run -it --rm ubuntu:24.04 bash -c 'id -u'
0

これを前提に環境を揃えて実行しても

$ docker container run -it --rm \
    -e DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus \
    -v /run/user/$(id -u)/bus:/run/user/0/bus \
    --security-opt apparmor=unconfined \
    kazuhira/notify:latest bash

接続が閉じられてしまいます。

# notify-send 'title' 'message'
The connection is closed

ubuntu:24.04に対してroot以外で実行するとパッケージのインストールができなくなるため、結局Dockerfileを書くことに
したという流れになります。

というわけで、ここまですればコンテナー内からデスクトップ通知ができました、と。

おわりに

Dockerコンテナー内で実行したnotify-sendコマンドから、ホスト側のUbuntu Linux 24.04 LTSにデスクトップ通知を行って
みました。

D-BusがUNIXドメインソケットを使っていることは調べるとすぐにわかったので、簡単にいくのではと思ったのですが
そうでもなかったですね。

まあ、ホスト側の環境を操作できるのはよろしくないですよと…。

ひとまず、やり方としてはわかりました。

Ubuntu Linux 24.04 LTSでデスクトップ通知をする(notify-sendコマンド)

これは、なにをしたくて書いたもの?

Ubuntu Linux 24.04 LTSでデスクトップ通知を行いたいなと思いまして。

なにかコードを書かないといけないと思っていたのですが、コマンドでもできるようです。

notify-sendコマンド

デスクトップ通知を行うには、notifiy-sendコマンドを使えばOKです。

Ubuntu Manpage: notify-send - a program to send desktop notifications

使い方は簡単で、サマリーとメッセージボディーを渡すだけで通知ができます。

そしてこのmanページを見るとわかるのですが、デスクトップ通知にはDesktop Notifications Specificationという仕様があります。

Desktop Notifications Specification

基本的な設計に関するドキュメントを見ると、通知はD-Busというものを使って実現していることがわかります。

In order to ensure that multiple notifications can easily be displayed at once, and to provide a convenient implementation, all notifications are controlled by a single session-scoped service which exposes a D-BUS interface.

Basic Design | Desktop Notifications Specification

D-Busはアプリケーション間が簡単に通信できるための、メッセージバスシステムです。システムDaemonとユーザーログイン
セッションごとのDaemonの2種類があります。

dbus

GNOMEの開発者向けには、各種言語向けのドキュメントもあったりします。

Using Notifications - GNOME Developer Documentation

Programming Languages - GNOME Developer Documentation

今回はnotify-sendコマンドをまずは試してみましょう。

環境

今回の環境はこちら。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.4 LTS
Release:        24.04
Codename:       noble


$ uname -srvmpio
Linux 6.8.0-107-generic #107-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 13 19:51:50 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux

notify-sendコマンドを使ってデスクトップ通知を行う

では、notify-sendコマンドを使ってデスクトップ通知を行ってみましょう。

notify-sendコマンドは、libnotify-binパッケージに含まれています。

$ apt-file search -F $(which notify-send)
libnotify-bin: /usr/bin/notify-send


$ apt show libnotify-bin
Package: libnotify-bin
Version: 0.8.3-1build2
Priority: optional
Section: utils
Source: libnotify
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian GNOME Maintainers <pkg-gnome-maintainers@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 44.0 kB
Depends: libc6 (>= 2.34), libglib2.0-0 (>= 2.62.0), libnotify4 (>= 0.7.10)
Task: ubuntu-desktop-minimal, ubuntu-desktop, kubuntu-desktop, kubuntu-full, xubuntu-minimal, xubuntu-desktop, ubuntu-mate-core, ubuntu-mate-desktop, ubuntu-budgie-desktop-minimal, ubuntu-budgie-desktop, ubuntu-budgie-desktop-raspi, ubuntu-unity-desktop, edubuntu-desktop-gnome-minimal, edubuntu-desktop-gnome, edubuntu-desktop-gnome-raspi
Download-Size: 9,936 B
APT-Manual-Installed: no
APT-Sources: http://jp.archive.ubuntu.com/ubuntu noble/main amd64 Packages
Description: 通知デーモンにデスクトップ通知を送信 (ユーティリティ)
 Desktop Notification 規格に定義されたデスクトップ通知を通知デーモンに送信
 するライブラリです。これらの通知は、ユーザにイベントを知らせたり、 ユーザの邪魔にならずにある形式の情報を表示するために利用できます。
 .
 本パッケージには、通知を送信するバイナリが含まれます。

バージョンを確認。

$ notify-send --version
notify-send 0.8.3

ヘルプ。

$ notify-send --help
用法:
  notify-send [オプション…] <SUMMARY> [BODY] - create a notification

ヘルプのオプション:
  -?, --help                        ヘルプのオプションを表示する

アプリケーションのオプション:
  -u, --urgency=LEVEL               Specifies the urgency level (low, normal, critical).
  -t, --expire-time=TIME            Specifies the timeout in milliseconds at which to expire the notification.
  -a, --app-name=APP_NAME           Specifies the app name for the icon
  -i, --icon=ICON                   Specifies an icon filename or stock icon to display.
  -c, --category=TYPE[,TYPE...]     Specifies the notification category.
  -e, --transient                   Create a transient notification
  -h, --hint=TYPE:NAME:VALUE        Specifies basic extra data to pass. Valid types are boolean, int, double, string, byte and variant.
  -p, --print-id                    Print the notification ID.
  -r, --replace-id=REPLACE_ID       The ID of the notification to replace.
  -w, --wait                        Wait for the notification to be closed before exiting.
  -A, --action=[NAME=]Text...       Specifies the actions to display to the user. Implies --wait to wait for user input. May be set multiple times. The name of the action is output to stdout. If NAME is not specified, the numerical index of the option is used (starting with 0).
  -v, --version                     Version of the package.

サマリーは必須で、あとは任意みたいですね。

試してみます。

$ notify-send 'こんにちは'

すると通知されました。簡単ですね。

本文も入れてみます。

$ notify-send 'こんにちは' '本文です'

そのまま表示されますね…。

通知のトレイで確認。

いくつかパターンを試してみた感じ、改行を含めることはムリそうです…。

オプションについてですが、指定しそうなのはアイコンでしょうか。

たとえば「terminal」を指定すると

$ notify-send -i terminal 'こんにちは' '本文です'

アイコンがターミナルのものになります。

「google-chrome」にすると

$ notify-send -i google-chrome 'こんにちは' '本文です'

Google Chromeのアイコンになります。

これは/usr/share/iconsディレクトリー内にある画像の名前を認識しているようで、こちらにもIcon Theme Specificationという
仕様があるようです。

Icon Theme Specification

任意の画像を指定することもできますが、この場合は絶対パスで指定する必要がありそうです。

$ notify-send -i /path/to/logo-mysql-170x115.png 'こんにちは' '本文です'

こんなところでしょうか。

おわりに

notify-sendコマンドを使って、Ubuntu Linux 24.04 LTSでデスクトップ通知をしてみました。

意外と簡単に使えましたが、仕様までしっかり決まっているようで驚きました。

Linuxデスクトップ環境はたくさんありますからね、確かにこういうのが要りますね…。