制作可使用ssh访问的Docker镜像,并使用Xmanger可视化访问

环境说明

因为一些原因的需要,需要在windows10下安装Docker,并想通过SSH服务可视化访问当前的Ubuntu镜像。

运行镜像

1 先对Docker进行设置,配置好加速器,然后使用docker pull ubuntu:对应版本,比如docker pull ubuntu:18.04,这样的方式下载相应的镜像
2 下载完成后,通过命令行

docker run \
       --name 18.04 \
       --privileged \
       --security-opt seccomp=unconfined \
       -d \
       -v d::/root/d \
       -v e::/root/e \
       -v f::/root/f \
       -p 1235:22 \
       ubuntu:18.04 \
       tail -f /dev/null

运行Docker,这里我贴出来我的命令,相应的选项大家可以根据自己的需要修改。这里执行tail -f /dev/null的原因是为了让Docker继续在后台运行,否则刚启动起来Docker容器的进程就会停止。
3 因为原生的镜像是精简过的,所以里面有很多东西都没有。不过想要让镜像变得完整,可以找到/usr/local/sbin/unminimize脚本(我目前只在18.04的镜像中看到该脚本),其内容如下:

#!/bin/sh

set -e

echo "This system has been minimized by removing packages and content that are"
echo "not required on a system that users do not log into."
echo ""
echo "This script restores the content and packages that are found on a default"
echo "Ubuntu server system."
echo ""
echo "Reinstallation of packages may fail due to changes to the system"
echo "configuration, the presence of third-party packages, or for other"
echo "reasons."
read -p "Would you like to continue? [y/N]" REPLY
echo    # (optional) move to a new line
if [ "$REPLY" != "y" ] && [ "$REPLY" != "Y" ]
then
    exit 1
fi

if [ -f /etc/dpkg/dpkg.cfg.d/excludes ] || [ -f /etc/dpkg/dpkg.cfg.d/excludes.dpkg-tmp ]; then
    echo "Re-enabling installation of all documentation in dpkg..."
    if [ -f /etc/dpkg/dpkg.cfg.d/excludes ]; then
        mv /etc/dpkg/dpkg.cfg.d/excludes /etc/dpkg/dpkg.cfg.d/excludes.dpkg-tmp
    fi
    echo "Updating package list and upgrading packages..."
    apt-get update
    # apt-get upgrade asks for confirmation before upgrading packages to let the user stop here
    apt-get upgrade
    echo "Restoring system documentation..."
    echo "Reinstalling packages with files in /usr/share/man/ ..."
    # Reinstallation takes place in two steps because a single dpkg --verified
    # command generates very long parameter list for "xargs dpkg -S" and may go
    # over ARG_MAX. Since many packages have man pages the second download
    # handles a much smaller amount of packages.
    dpkg -S /usr/share/man/ |sed 's|, |\n|g;s|: [^:]*$||' | DEBIAN_FRONTEND=noninteractive xargs apt-get install --reinstall -y
    echo "Reinstalling packages with system documentation in /usr/share/doc/ .."
    # This step processes the packages which still have missing documentation
    dpkg --verify --verify-format rpm | awk '/..5......   \/usr\/share\/doc/ {print $2}' | sed 's|/[^/]*$||' | sort |uniq \
         | xargs dpkg -S | sed 's|, |\n|g;s|: [^:]*$||' | uniq | DEBIAN_FRONTEND=noninteractive xargs apt-get install --reinstall -y
    if dpkg --verify --verify-format rpm | awk '/..5......   \/usr\/share\/doc/ {exit 1}'; then
        echo "Documentation has been restored successfully."
        rm /etc/dpkg/dpkg.cfg.d/excludes.dpkg-tmp
    else
        echo "There are still files missing from /usr/share/doc/:"
        dpkg --verify --verify-format rpm | awk '/..5......   \/usr\/share\/doc/ {print " " $2}'
        echo "You may want to try running this script again or you can remove"
        echo "/etc/dpkg/dpkg.cfg.d/excludes.dpkg-tmp and restore the files manually."
    fi
fi

if ! dpkg-query --show --showformat='${db:Status-Status}\n' ubuntu-minimal 2> /dev/null | grep -q '^installed$'; then
    echo "Installing ubuntu-minimal package to provide the familiar Ubuntu minimal system..."
    DEBIAN_FRONTEND=noninteractive apt-get install -y ubuntu-minimal
fi

# unminimization succeeded, there is no need to mention it in motd
rm -f /etc/update-motd.d/60-unminimize

4 如果没有这个脚本,那么就需要自己手动安装自己需要的软件。这里需要对软件源进行配置,如何配置可以参考该博客

以SSH的方式运行

1 先安装相应的SSH服务,这里我选择的是openssh-server进行安装。

apt install openssh-server

2 安装完成后,可以在容器中测试一下自己的账号是否能进行访问。比如,看ssh root@localhost是否能成功访问。
3 如果使用的root用户,且没有设置密码,那么是无法使用SSH进行访问。这就需要设置一下root用户的密码,直接输入passwd就可以修改root用户的密码。(亲测passwd root修改是无效的)
4 测试没问题之后,可以提交一下Docker镜像保存一下已经安装的软件和修改的配置。然后,将当前运行的容器删除。
5 将之前运行Docker的命令行进行修改,将tail -f /dev/null修改成/usr/sbin/sshd -D,其意思是在后台运行SSH服务。
6 因为我在上面将主机端的1235端口映射到容器端的22端口,所以这时我只需要键入ssh -p 1235 root@localhost,之后输入对应的密码就可以。
7 如果使用Xshell访问的话,命令行有所不同,ssh root@localhost 1235即可访问当前运行的Docker。

以图形化方式访问

1 首先安装Xmanager软件,我当前装的版本是6,因为还是测试版,所以免费使用。
2 需要先通过SSH访问运行的Docker,安装一些必要的软件

apt install xorg openbox xauth rxvt-unicode -y

3 参考该博客修改ssh的配置文件。
4 参考该博客设置xauth。
5 如果遇到类似Can't open display: localhost:11.0的问题,可以参考该博客
6 安装对应的可视化软件,比如xclock,安装x11-apps即可。

apt install x11-apps

7 打开Xmanger文件夹,在Tools中找到Xstart,对“主机”,“协议”,“用户名”和“身份验证”进行配置后,在命令中输入

LC_ALL=en_US.UTF-8 /usr/bin/urxvt

使用密码或其他加密方式就可以登录Docker镜像内部了。
8 测试,执行xclock,看是否能出现图形显示。如果出现了,那么就成功了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值