Skip to content

Instantly share code, notes, and snippets.

@yen3
Created July 5, 2022 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yen3/b5fcdfffd441ab43c401ad108ed5fc72 to your computer and use it in GitHub Desktop.
Save yen3/b5fcdfffd441ab43c401ad108ed5fc72 to your computer and use it in GitHub Desktop.
A tiny example: Execute "notify-send" from container to send messages to host
#!/usr/bin/env bash
# ref: https://forums.docker.com/t/communicate-with-dbus-from-an-unprivileged-container/101549
# ref: https://stackoverflow.com/questions/23601844/how-to-create-user-in-linux-by-providing-uid-and-gid-options
# ref: https://github.com/mviereck/x11docker/wiki/How-to-connect-container-to-DBus-from-host
set -euxo pipefail
readonly DBUS_USER_SESSION_PATH="${DBUS_SESSION_BUS_ADDRESS#"unix:path="}"
readonly uid=$(id -u)
readonly gid=$(id -g)
docker run -it \
--rm \
--privileged \
--security-opt "apparmor:unconfined" \
--cap-add ALL \
-v /var/run/dbus:/var/run/dbus \
-e DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
-v "${DBUS_USER_SESSION_PATH}:${DBUS_USER_SESSION_PATH}" \
ubuntu:22.04 \
/bin/bash -c "apt update && apt install -y libnotify-bin && groupadd -g ${gid} user && useradd user -u ${uid} -g ${gid} -m -s /bin/bash && su user -s /bin/bash -c '/usr/bin/notify-send test-1'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment