5

docker容器中跑GUI的最简单方法

 2 years ago
source link: https://www.lujun9972.win/blog/2018/04/24/docker%E5%AE%B9%E5%99%A8%E4%B8%AD%E8%B7%91gui%E7%9A%84%E6%9C%80%E7%AE%80%E5%8D%95%E6%96%B9%E6%B3%95/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

docker容器中跑GUI的最简单方法

最简单的方法就是让docker容器中的xclient应用直接使用主机上的Xserver就好了。

步骤上就两步

  1. 放开权限,允许本地连接

    xhost +local:
    
  2. /tmp/.X11-unix 共享到容器中

    docker run -it --rm -v /tmp/.X11-unix:/tmp/.X11-unix ${docker_image}
    

当然,你也可以在放开权限时指定容器主机名/或者在运行容器时指定与主机相同的名称,这样会安全一些,只是步骤上会麻烦一点

xhost +local:$(docker inspect --format='{{ .Config.Hostname }}' $containerId)
docker start $containerId
docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --hostname=$HOSTNAME -v $HOME/.Xauthority:/root/.Xauthority -it --rm ${docker_image}

=/tmp/.X11-unix=是什么玩意?

X11 server需要有一种途径来跟X11 client来进行沟通。 在网络上它们可以通过TCP/IP Socket来实现沟通,而在本机上它们通过一个Unix-domain socket来沟通.

Unix-domain socket其实很TCP/IP socket很类似,只不过它指向的是一个文件路径,而且无需通过网卡进行转发,因此相对来说更安全,更更快些。

/tmp/.X11-unix 其实就是存放这些Unix-domain Socket的地方。

一般来说 /tmp/.X11-unix 下面只会有一个 Unix-domain Socket(因为一般只有一个Xserver在运行),但若系统同时运行多个Xserver,也可能会有多个Unix Domain Socket出现的情况。

ls -l /tmp/.X11-unix
总用量 0
srwxrwxrwx 1 lujun9972 lujun9972 0 4月  24 09:18 X0

注意到 X0 前面的 s ,说明它是一个socket文件。

我们可以使用 lsof -U 来查看有那些进程在使用这个 Unix-domain Socket

lsof -U /tmp/.X11-unix/X0 |cut -f1 -d " " |uniq
COMMAND
systemd
xinit
Xorg
awesome
fcitx
udiskie
python2
dbus-daem
fcitx-dbu
java
gvfsd
gvfsd-fus
at-spi-bu
dbus-daem
at-spi2-r
emacs
firefox
Web\x20Co
gconfd-2
Web\x20Co
urxvt
pulseaudi

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK