Docker+MobaXterm+x11实现容器UI界面转发本地

本文记录了搭建一个可直接ssh访问的container,并可通过x11转发界面的实现过程

0.1 实验环境

PC:windows 11

Server:Ubuntu 18.04

Docker image:Ubuntu 18.04

  1. 获取Ubuntu 18.04的镜像

使用Dockerfile获取镜像,对应的文件内容如下

Get the base Ubuntu image from Docker Hub

FROM ubuntu:18.04

FROM continuumio/anaconda3:latest

LABEL maintainer="hello@hello.edu"

ENV DEBIAN_FRONTEND=noninteractive

Update apps on the base image

RUN apt-get -y update

Packages

RUN apt-get install -y autoconf automake autotools-dev curl libmpc-dev libmpfr-dev \

libgmp-dev gawk build-essential bison flex texinfo gperf \

libtool patchutils bc libqt4-dev python-dev flex bison \

libgoogle-perftools-dev python-six libssl-dev zlib1g-dev \

zip unzip zsh tmux wget git openssh-client vim emacs \

default-jdk default-jre

RUN echo "deb https://dl.bintray.com/sbt/debian /" | tee -a /etc/apt/sources.list.d/sbt.list && \

curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get\&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | apt-key add && \

apt-get update && \

apt-get install sbt

在Dockerfile的同一目录下执行如下命令即可

docker build -t Ubuntu:18.04 .

  1. 使用镜像生成容器并配置端口转发

我们的需求是,可以直接通过ssh访问生成的ubuntu 18.04 container,因此需要将容器的ssh端口暴露到server的某个端口上,这样做有很多好处,最主要的是直接通过Xterm上传下载container中的文件,而不用使用docker cp命令,同时也可以支持x11转发UI界面。使用的命令如下

docker run -it --privileged=true -p 50002:22 --hostname=<host name> --name=<container name> Ubuntu:18.04 /bin/bash

这里将容器的22端口转发给server的50002端口,以后直接ssh server:50002就可以直接进这个container

Note:退出重进container的命令如下

sudo docker start <container name>

sudo docker attach <container name>

更新美化Ubuntu的一系列操作可见我另一篇博文

  1. 配置ssh

需要重新安装一下ssh,我碰到的问题是没有sshd\_config文件,使用如下命令

rm -rf /etc/ssh

apt install ssh

然后配置ssh支持x11转发

echo "Port 22">>/etc/ssh/sshd_config

echo "PermitRootLogin yes">>/etc/ssh/sshd_config

echo "X11Forwarding yes" >> /etc/ssh/sshd_config

重新启动ssh服务

service ssh restart

查看ssh服务状态

service ssh status

接下来设定root的密码,以便远程登陆

passwd

  1. 配置界面转发

到这里为止,已经可以使用Xterm登录了,但是界面转发还是问题,需要指明转发目标,增加如下环境变量即可

export DISPLAY=:<Your PC IP Addr>:0.0

相关推荐
cpolar技术支持6 小时前
用一份 YAML 画出家里的网络拓扑:哪个口通公网、哪几个容器藏在内网,一张图摊开,再用 cpolar 在外网随时翻这份图
docker·网络拓扑·内网穿透·cpolar·yaml
赤生云电竞9 小时前
算力调度系统架构实践:计费、配额与抢占策略设计——面向Kubernetes GPU集群的多租户治理
容器·系统架构·kubernetes
FriendshipT12 小时前
YOLOE Docker 部署实战:三模式推理、掩码输出与跨图视觉的完整实现
运维·人工智能·python·深度学习·docker·容器
the局外人12 小时前
Docker 部署:从镜像构建到容器上线
后端·docker·容器
方便面不加香菜1 天前
Docker Compose 文件 -- docker-compose.yml
spring cloud·docker·容器
你的钉钉响了1 天前
用 kubeadm-ha 部署高可用 Kubernetes 集群
云原生·容器·kubernetes·ansible·高可用集群·kubeadm-ha
lambo mercy2 天前
04Docker_Container_容器
docker
Wellijohn2 天前
Docker 瘦身、手写 MCP、异步并发:被问得最多的 12 个问题,一次答完
python·docker·mcp
隔窗听雨眠2 天前
KWDB容器化部署的形态选择与参数细节
docker·时序数据库·kwdb
SeanQhl2 天前
openEuler 下 Docker 镜像操作与多阶段构建瘦身
docker·容器·镜像·openeuler·dockerfile