docker基于commit方式为镜像添加SSH服务
下载启动镜像
docker pull ubuntu: 18.04
docker run -it ubuntu:18.04 bash
配置软件源
备份/etc/apt/sources.list
#备份
cp /etc/apt/sources.list /etc/apt/sources.list.bak
在/etc/apt/sources.list文件前面添加如下条目(可以采用echo "xxx" > sources.list方式添加)
#添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
安装和配置ssh服务
ubuntu容器中默认没有vim
apt-get install vim
apt-get install openssh-server
mkdir root/.ssh
vim /root/.ssh/authorized_keys
将本机和宿主机的授权key都加到~/.ssh/authorized_keys。
创建启动ssh服务脚本
vim /run.sh
chmod +x run.sh
#!/bin/bash
/usr/sbin/sshd -D
exit
取消pam登陆限制
sed ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd
提交新的镜像
docker commit 8945ee8845fd sshd1:ubuntu
启动镜像
docker run -p 10022:22 -d sshd1:ubuntu /run.sh
本机ssh访问测试
可以看到是免密登录了。