在 Docker 容器中使用 VNCserver + GUI 应用程序 + 虚拟显示 [英] Using VNCserver + GUI application + Virtual Display in Docker container

查看:87
本文介绍了在 Docker 容器中使用 VNCserver + GUI 应用程序 + 虚拟显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 docker 容器中运行 firefox(或任何图形应用程序).

I would like to run firefox (or any graphical application) inside docker container.

我的要求:当我启动容器时,我应该创建一个虚拟显示器,启动 VNC 服务器,然后启动 GUI 应用程序.这意味着在容器成功启动后,我可以通过 VNC 客户端连接到容器内运行的 GUI 应用程序.当我关闭应用程序时,容器应该会自动停止.

My requirement: When I start the container, I should be creating a virtual display, starting VNC server and then the GUI application. This means after the container is successfully started, I can connect to the GUI application running inside the container via VNC client. When I close the application, the container should automatically stop.

尝试 1:我从这里的示例开始 https://hub.docker.com/r/devopsil/vnc-firefox/~/dockerfile/在此示例中,开发人员通过 CMD 启动 vnc 服务器并使其永久运行.他将 firefox 作为 .rc 文件的一部分,以便在容器启动时启动.如果您通过 VNC 客户端连接,您可以看到 firefox 并且按预期关闭 firefox 或断开 VNC 会话不会停止容器.

Attempt 1: I started with the example here https://hub.docker.com/r/devopsil/vnc-firefox/~/dockerfile/ In this example the developer starts the vnc server via CMD and keeps it running forever. He puts the firefox as part of .rc file so it starts when the container starts. If you connect via VNC client, you can see firefox and as expected closing firefox or disconnecting the VNC session does not stop the container.

尝试 2:我按如下方式调整了 Dockerfile.仅更改了最后一行(将 CMD 替换为 ENTRYPOINT 并删除了永远"标志")

Attempt 2: I tuned the Dockerfile as follows. Changed only the last line (replaced CMD with ENTRYPOINT and removed the "forever" flag")

ENTRYPOINT ["/usr/bin/x11vnc", "-usepw", "-create"]

重建和运行容器docker run --rm -p 5900"现在有不同的行为:容器启动,通过 VNC 客户端的连接正常工作,firefox 在几秒钟后打开(等待消息extension RANDR missing on display :20"),容器在关闭 vnc 连接后自动停止.

Rebuilding and running the container "docker run --rm -p 5900 " had a different behaviour now: container starts, connection via VNC client worked, firefox opened after a few seconds (wait after the message "extension RANDR missing on display :20") and the container stopped automatically after closing the vnc connection.

尝试 3:然后我创建了一个 bash 脚本来启动 vncserver 和 firefox 并更改了 Dockerfile 以将这个脚本文件复制到映像中并将该脚本作为入口点.但我收到类似于未设置显示变量"的错误

Attempt 3: Then I created a bash script to start the vncserver and firefox and changed the Dockerfile to copy this script file inside the image and made the script as the entry point. But I got the error similar to "Display variable not set"

#!/bin/bash
/usr/bin/x11vnc -forever -usepw -create &
firefox

尝试 4:所以我通过 docker run 命令传递了 DISPLAY 变量,但是错误是无法打开显示.

Attempt 4: So I passed the DISPLAY variable via the docker run command but then the error is cannot open display.

问题:我不知道如何将vncserver标志-create"创建的虚拟显示喜欢到firefox.请让我知道在容器启动时启动 GUI 应用程序并在关闭 GUI 应用程序时自动停止容器的最佳方式.我相信dockerfile中的ENTRYPOINT应该设置为firefox.感谢您的帮助.

Question: I don’t know how to like the virtual display created by the vncserver flag "-create" to the firefox. Please let me know what would be best way to start the GUI application when the container starts and automatically stop the container when the GUI application is closed. I believe the ENTRYPOINT in dockerfile should be set to firefox. Thanks for your help.

我看过这个 https://blog.jessfraz.com/post/docker-containers-on-the-desktop/ 但这对我没有用,因为我想通过 VNC 远程访问应用程序.

I have seen this https://blog.jessfraz.com/post/docker-containers-on-the-desktop/ but this is of no use to me as I want to access the application remotely via VNC.

推荐答案

我设法找到了解决方案:

I managed to found the solution:

将上述尝试 3 中的脚本更改为如下工作

Changed the script in Attempt 3 above as follows worked

!/bin/bash

Xvfb :1 -screen 0 800x600x16 &
/usr/bin/x11vnc -display :1.0 -usepw &
DISPLAY=:1.0
export DISPLAY
firefox

干杯.

这篇关于在 Docker 容器中使用 VNCserver + GUI 应用程序 + 虚拟显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆