在 Windows 主机上的 Linux Docker 容器中运行 GUI 应用?这是什么意思?为什么有人会尝试这么做?如何从 Windows 主机共享显示?

2025-06-04

在 Windows 主机上的 Linux Docker 容器中运行 GUI 应用程序

这是关于什么的?

为什么有人会尝试这么做?

如何从 Windows 主机共享显示?

这是关于什么的?

Docker 有很多优势,其中之一就是能够在 Docker 容器中使用带有独立 GUI 的应用程序。例如浏览器、文本编辑器或其他程序。

不用说,这将使你能够在 Windows 主机上使用 Linux / macOS 软件,而无需进行任何黑客攻击。此外,由于应用程序仍然封装在 Docker 容器中,因此在删除应用程序时,你的机器不会残留任何依赖项。

为什么有人会尝试这么做?

我在家的私人电脑上使用 Arch Linux,在公司使用 Windows 10。我希望能够在我的 Windows 电脑上使用 Evolution 邮件客户端和其他方便的 Linux 应用。

因此,关于如何从 Linux 主机向 Linux 容器共享 X11 会话,有很多教程。但是:

如何从 Windows 主机共享显示?

安装 VcXsrv 并配置它

首先,安装VcXsrv Windows X Server。我们也可以使用 Xming,但是 Windows 版的软件包自 2013 年以来就没有更新过。最简单的方法是使用Chocolatey,顺便说一下,它是我最喜欢的 Windows 软件包管理器!
所以,启动一个 powershell 会话并运行:

choco install vcxsrv
Enter fullscreen mode Exit fullscreen mode

然后从开始菜单运行Xlaunch,并按照初始配置步骤操作:在点击“完成”之前,请务必保存配置文件! 将其保存到以下位置之一:
设置1
设置2
设置3
设置4

  • %appdata%\Xming
  • %userprofile%\桌面
  • %用户配置文件%

创建 Dockerfile

举一个简单的例子,创建一个新文件夹并在其中放置一个包含以下内容的Dockerfile :

FROM ubuntu:14.04
RUN apt-get update && apt-get install -y firefox
CMD /usr/bin/firefox
Enter fullscreen mode Exit fullscreen mode

构建并运行容器

对于高级 docker 用户,这里有快速命令:

docker build -t firefox .
set-variable -name DISPLAY -value YOUR-IP:0.0
docker run -ti --rm -e DISPLAY=$DISPLAY firefox
Enter fullscreen mode Exit fullscreen mode

有一些解释:

现在构建新的容器并将其标记为firefox

docker build -t firefox .
Enter fullscreen mode Exit fullscreen mode

由于容器有自己的 localhost 接口,我们需要使用网络适配器的 IP 地址。
使用以下命令获取你的 IP 地址:

ipconfig
Enter fullscreen mode Exit fullscreen mode

设置环境变量(将 IP 替换为您的):

set-variable -name DISPLAY -value 10.11.128.118:0.0
Enter fullscreen mode Exit fullscreen mode

运行容器:

docker run -ti --rm -e DISPLAY=$DISPLAY firefox
Enter fullscreen mode Exit fullscreen mode

您现在应该看到一个 Firefox 窗口:
Firefox 码头工人

这对于大多数其他应用程序都适用。

持久化数据

为了保存数据(例如进化邮件),请通过 docker 卷安装正确的位置来启动容器。

找出您的应用存储要保留的数据的所有位置并挂载它们:

docker run -v c:/docker/evolutionmail/home/user/.local/share/evolution:/root/.local/share/evolution -v c:/docker/evolutionmail/home/user/.config/evolution:/root/.config/evolution -ti --rm -e DISPLAY=$DISPLAY evolution
Enter fullscreen mode Exit fullscreen mode

进化邮件的示例 Dockerfile

以下是我的进化 Dockerfile,以防有人需要它:

FROM debian
# Setup enviroment variables
ENV DEBIAN_FRONTEND noninteractive

# Installing fuse filesystem is not possible in docker without elevated priviliges
# but we can fake installling it to allow packages we need to install for GNOME
RUN apt-get install libfuse2 -y && \
cd /tmp ; apt-get download fuse && \
cd /tmp ; dpkg-deb -x fuse_* . && \
cd /tmp ; dpkg-deb -e fuse_* && \
cd /tmp ; rm fuse_*.deb && \
cd /tmp ; echo -en '#!/bin/bash\nexit 0\n' > DEBIAN/postinst && \
cd /tmp ; dpkg-deb -b . /fuse.deb && \
cd /tmp ; dpkg -i /fuse.deb

# Upstart and DBus have issues inside docker.
RUN dpkg-divert --local --rename --add /sbin/initctl && ln -sf /bin/true /sbin/initctl

# Install GNOME
RUN apt-get update && apt-get install -y xorg gnome-core gnome-session-fallback
# Pull in the hack to fix keyboard shortcut bindings for GNOME 3 
ADD https://raw.githubusercontent.com/CannyComputing/Dockerfile-Ubuntu-Gnome/master/gnome-keybindings.pl /usr/local/etc/gnome-keybindings.pl
RUN chmod +x /usr/local/etc/gnome-keybindings.pl

# Add the script to fix and customise GNOME for docker
ADD https://raw.githubusercontent.com/CannyComputing/Dockerfile-Ubuntu-Gnome/master/gnome-docker-fix-and-customise.sh /usr/local/etc/gnome-docker-fix-and-customise.sh
RUN chmod +x /usr/local/etc/gnome-docker-fix-and-customise.sh

RUN apt-get update -y && apt-get install -y dbus-x11 gnome-keyring evolution evolution-data-server seahorse sudo
RUN apt-get install -y libnotify-cil-dev
CMD ["evolution"]
Enter fullscreen mode Exit fullscreen mode

我很好奇想看看您在评论中 dockerized 了什么!

~干杯

文章来源:https://dev.to/darksmile92/run-gui-app-in-linux-docker-container-on-windows-host-4kde
PREV
React Native Web React 开发者们,为什么不在 Web 上使用 React Native?你是 React 开发者吗?为什么不在 Web 上使用 React Native?
NEXT
Windows 上的 Linux:通过 RDP 实现桌面环境的 WSL