「Linux Desktop」- 在 Docker 中,运行桌面应用(解决依赖问题)

  CREATED BY JENKINSBOT

问题描述

我们使用 Debian 发行版,但是某些软件只能用于 Ubuntu 系统,或者其他 Linux 发行版(比如向日葵远程控制)。

我们可以通过 Chroot 模拟环境(环境制作繁琐),或者通过 Docker 来解决该问题。

该笔记将记录:在 Debian 10 中,通过 Ubuntu 18.04 Docker Image 来运行 XTerm 程序。

解决方案

第一步、创建 Dockerfile 文件

FROM ubuntu

RUN apt-get update -y && apt-get install -y x11-apps xterm

第二步、构建镜像

docker build -f Dockerfile.xterm -t xterm-within-docker ./

第三步、启动容器以运行程序

docker run --rm --net=host --env="DISPLAY" \
    --volume=/run/user/1001/gdm/Xauthority:/root/.Xauthority:rw \
    xterm-within-docker \
    xterm

注意事项,各个发行版创建的 /run/user/1001/gdm/Xauthority 路径各不相同,可以使用以下命令进行确定:

printenv XAUTHORITY

因此 docker run 命令可以改写为:

...
--volume=$(printenv XAUTHORITY):/root/.Xauthority:rw
...

第四步、验证当前终端为容器内

# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.1 LTS"

我们的系统为 Debian GNU/Linux 10 (buster) 而输出为 Ubuntu 20.04.1 LTS 表示成功。

应用案例

如果仅仅能够在 Docker Container 中运行 Xterm 是没有意义的,我们有以下应用场景:
1)我们需要在 Debian 10 中运行 应用,但是无法满足依赖关系。我们通过该方法解决这一问题:Sunlogin

参考文献

Running GUI apps with Docker | Fabio Rehm’s Blog
Running GUI Applications inside Docker Containers | by Saravanan Sundaramoorthy | Medium
x11 – What is the best way to find the current DISPLAY and XAUTHORITY in non interactive shell for the current user? – Unix & Linux Stack Exchange
jrei/systemd-ubuntu – Docker Hub
jrei/systemd-ubuntu Dockerfile – Docker Hub