Macos通过SSH连接显示docker容器的GUI界面
服务器没有gui界面,但是项目中需要接实时视频流进行可视化。所以只能远程连接通过X11进行转发到本机可视化。
一、配置远程服务器(UBUNTU)
1、安装SSH
apt-get install openssh-server
2、配置SSH服务端
vim /etc/ssh/sshd_config
# 修改以下内容,端口可以自己设置,默认是22,和启动容器的映射端口对应
PermitRootLogin yes
X11Forwarding yes
附上一份自己的配置:
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM no
3、重启服务
/etc/init.d/ssh restart
4、安装xorg
sudo apt install xorg
二、本地Macos配置
1、安装XQuartz
由于macOS在新版中去掉了x11的支持,Mac 不再随附 X11,但 XQuartz 项目会提供 X11 服务器和客户端库。所以我们需要安装XQuartz。
2、配置SSH客户端
vim /private/etc/ssh/ssh_config
# 修改以下参数
ForwardX11 yes
三、连接服务器
打开XQuartz的xterm
# 允许任何机器连接
xhost +
# 连接远程服务器
ssh -X 用户名@ip -p 22
# 如果是docker容器则需要设置和主进程相同的环境变量
export $(cat /proc/1/environ |tr '\0' '\n' | xargs)
# 使用时钟进行测试
apt install xarclock
xarclock
# 运行python脚本
python XXX.py
四、启动Docker容器
# 自己启动容器的命令
nvidia-docker run -it -v "$HOME/.Xauthority:/root/.Xauthority:rw" -v /disk5/wjj:/home/wjj --net=host -v $HOME/slides:/root/slides --env DISPLAY="$DISPLAY" -p 9900:9900 -p 9393:9393 --name pytorch_gui pytorch_gui:1.0 /bin/bash