これは、なにをしたくて書いたもの?
notify-sendコマンドを使って、デスクトップ通知を行う方法を調べました。
Ubuntu Linux 24.04 LTSでデスクトップ通知をする(notify-sendコマンド) - CLOVER🍀
今回はDockerコンテナー内からホスト側のデスクトップ環境に通知をする方法を調べてみます。
デスクトップ通知とD-Bus
デスクトップ通知にはD-Busというメッセージバスシステムを使っています。
Basic Design | Desktop Notifications Specification
また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.
こちらを使って、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ドメインソケットを使っていることは調べるとすぐにわかったので、簡単にいくのではと思ったのですが
そうでもなかったですね。
まあ、ホスト側の環境を操作できるのはよろしくないですよと…。
ひとまず、やり方としてはわかりました。





