如何使用 Qt 和 D-Bus 在 Docker 容器中正确运行图形应用程序?

如何使用 Qt 和 D-Bus 在 Docker 容器中正确运行图形应用程序?

我试图获得 Audex跑步20.04 LTS 主机上的 16.04 LTS Docker 容器内。

下面的方法对于基于 Gtk 的应用程序可以正常工作,我已经测试过了。

在当前情况下,我做了以下事情:

sudo apt-get update
sudo apt-get install docker.io
sudo usermod -a -G docker $USER
# reboot

mkdir ~/docker-audex
cat > ~/docker-audex/Dockerfile << EOF
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install -y audex
CMD audex
EOF

docker build -t ubuntu:audex ~/docker-audex

然后尝试从终​​端启动它并得到输出:

$ docker run -e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --user="$(id --user):$(id --group)" ubuntu:audex 
QDBusConnection: session D-Bus connection created before QCoreApplication. Application
may misbehave. audex(7): KUniqueApplication: Cannot find the D-Bus
session server:  "/usr/bin/dbus-launch terminated abnormally with the
following error: Autolaunch error: X11 initialization failed. " 

audex(6): KUniqueApplication: Pipe closed unexpectedly.  

或者

$ docker run -e DISPLAY -e DBUS_SESSION_BUS_ADDRESS -v /tmp/.X11-unix:/tmp/.X11-unix --user="$(id --user):$(id --group)" ubuntu:audex

QDBusConnection: session D-Bus connection created before
QCoreApplication. Application may misbehave. audex(7):
KUniqueApplication: Cannot find the D-Bus session server:  "Failed to
connect to socket /run/user/1000/bus: No such file or directory" 

audex(6): KUniqueApplication: Pipe closed unexpectedly.```

或者

$ docker run -e DISPLAY -e DBUS_SESSION_BUS_ADDRESS -v /tmp/.X11-unix:/tmp/.X11-unix -v /run:/run --user="$(id --user):$(id --group)" ubuntu:audex
QDBusConnection: session D-Bus connection created before QCoreApplication. Application may misbehave. audex(7):
KUniqueApplication: Cannot find the D-Bus session server:  "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)" 

audex(6): KUniqueApplication: Pipe closed unexpectedly.  

并且未显示应用程序窗口。

我猜这里的 D-Bus 有问题。我们该如何修复它?


更新,关注@SimonSudler回答如下

$ docker run -e DISPLAY -e DBUS_SESSION_BUS_ADDRESS     -v /tmp/.X11-unix:/tmp/.X11-unix -v /run:/run     --user="$(id --user):$(id --group)"     --security-opt apparmor=unconfined     ubuntu:audex

QDBusConnection: session D-Bus connection created before QCoreApplication. Application may misbehave.
QDBusConnection: session D-Bus connection created before QCoreApplication. Application may misbehave.
Error: Can not find password entry for uid 1000.
trying to create local folder //.kde: Permission denied
audex(7)/KSharedDataCache KSharedDataCache::Private::mapSharedMemory: Failed to establish shared memory mapping, will fallback to private memory -- memory usage will increase 
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -f, --force                  do not prompt before overwriting
  -i, --interactive            prompt before overwrite
  -n, --no-clobber             do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -Z, --context                set SELinux security context of destination
                                 file to default type
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report mv translation bugs to <http://translationproject.org/team/>
Full documentation at: <http://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'
audex(7)/kdecore (KConfigSkeleton) KCoreConfigSkeleton::writeConfig:
Error: Can not find password entry for uid 1000.
X Error: BadAccess (attempt to access private resource denied) 10
  Extension:    130 (MIT-SHM)
  Minor opcode: 1 (X_ShmAttach)
  Resource id:  0x12d
X Error: BadShmSeg (invalid shared segment parameter) 128
  Extension:    130 (MIT-SHM)
  Minor opcode: 5 (X_ShmCreatePixmap)
  Resource id:  0x3a00017
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
  Major opcode: 62 (X_CopyArea)
  Resource id:  0x3a00018
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
  Major opcode: 62 (X_CopyArea)
  Resource id:  0x3a00018
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
  Major opcode: 62 (X_CopyArea)
  Resource id:  0x3a00018
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
  Major opcode: 62 (X_CopyArea)
  Resource id:  0x3a00018

添加--ipc=host也无济于事:

$ docker run --ipc=host -e DISPLAY -e DBUS_SESSION_BUS_ADDRESS     -v /tmp/.X11-unix:/tmp/.X11-unix -v /run:/run     --user="$(id --user):$(id --group)"     --security-opt apparmor=unconfined     ubuntu:audex
QDBusConnection: session D-Bus connection created before QCoreApplication. Application may misbehave.
QDBusConnection: session D-Bus connection created before QCoreApplication. Application may misbehave.
Error: Can not find password entry for uid 1000.
trying to create local folder //.kde: Permission denied
audex(7)/KSharedDataCache KSharedDataCache::Private::mapSharedMemory: Failed to establish shared memory mapping, will fallback to private memory -- memory usage will increase 
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -f, --force                  do not prompt before overwriting
  -i, --interactive            prompt before overwrite
  -n, --no-clobber             do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -Z, --context                set SELinux security context of destination
                                 file to default type
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report mv translation bugs to <http://translationproject.org/team/>
Full documentation at: <http://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'
audex(7)/kdecore (KConfigSkeleton) KCoreConfigSkeleton::writeConfig:
Error: Can not find password entry for uid 1000.

答案1

运行不使用 apparmor 配置文件的容器

原因是docker-defaultapparmor 的配置文件。规则通过以下方式集成这个模板进入docker守护进程。

有一种快速验证方法,如果该配置文件导致问题,请将其添加--security-opt apparmor=unconfined到命令中run

docker run -e DISPLAY -e DBUS_SESSION_BUS_ADDRESS \
    -v /tmp/.X11-unix:/tmp/.X11-unix -v /run:/run \
    --user="$(id --user):$(id --group)" \
    --security-opt apparmor=unconfined \
    -v /dev/sr0:/dev/sr0 \
    -v /dev/cdrom:/dev/cdrom \
    --privileged \
    ubuntu:audex

您可以开始为 audex 编写 apparmor 配置文件,但我认为这不值得。由于audix是 CD 翻录器,因此用户需要一些“特权”访问权限才能将 CD 插入/移除到 PC 中。因此使用apparmor=unconfined应该足够安全。

Audex 的核心转储

启动时audex,它会执行几个它认为是编码器的命令(例如faac --help)。出于某种原因,它还会执行mv --help,从而产生奇怪的输出。至少这似乎不是问题的一部分。此外,此行为仅在~/.kde/share/config/audexrc写入文件之前发生。此后,只剩下崩溃……

查看 audex 的核心转储:

gdb /usr/bin/audex core.audex.3225
...
(gdb) bt
#0  0x00007f8799abda7f in KUrl::KUrl(QString const&) () from /usr/lib/libkdecore.so.5
#1  0x00007f879b4c7245 in KCompactDisc::cdromDeviceUrl(QString const&) () from /usr/lib/libkcompactdisc.so.4
#2  0x0000000000423469 in MainWindow::MainWindow (this=0x23ef580, parent=<optimized out>, __in_chrg=<optimized out>, 
    __vtt_parm=<optimized out>) at /build/buildd/audex-0.78/mainwindow.cpp:39
#3  0x000000000041a159 in main (argc=3, argv=0x7ffd939ccf58) at /build/buildd/audex-0.78/main.cpp:46

我以为audix在尝试读取 CD-ROM 设备 URL 时会死机。我添加了和/dev/sr0/dev/cdrom适应这种情况,但没有任何效果(参见 docker 的 handbreak 示例)。另外,将audex容器中的用户添加到cdrom组也无济于事。

这似乎是audix16.04 版旧版本的一个错误,它需要一个遗留的 URL 字符串。

由于缺少一些“旧”的 KDE 行为,可能无法audexubuntu:16.04当前的 Ubuntu 系统上运行。不过Neon/Docker来自 KDE,但它也需要主机上有 KDE。

相关内容