环境说明
因为一些原因的需要,需要在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,看是否能出现图形显示。如果出现了,那么就成功了。