docker容器开启sshd服务,模拟服务器
我们知道docker是可以用exec来直接访问容器的,但是还不够high,有时候要模拟服务器的登录总不能用docker exec吧,来吧,老司机带你飞!
以centos为例,需要几步操作
1.安装openssh-server
2.初始化root用户密码
3.开启sshd服务
废话不多说,dockerfile献上
FROM centos RUN yum install -y wget && \ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \ yum install -y passwd && \ yum install -y openssh-server ssh-keygen && \ echo 'abcd1234' | passwd root --stdin RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" -q && \ ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" -q && \ ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" -q #RUN systemctl enable sshd CMD /usr/sbin/sshd && tail -f /var/log/wtmp
简单解释一下,
- 安装openssh-server
yum install -y wget && \ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \ yum install -y passwd && \ yum install -y openssh-server ssh-keygen
- 修改root密码为abcd1234
echo 'abcd1234' | passwd root --stdin
- 创建ssh-keygen创建相关的ssh文件,-q的意思是静默模式(就是默认是需要让你回车输入的,加上这个直接跳过)
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" -q && \ ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" -q && \ ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" -q
- 开启sshd服务,并用tail来前台执行阻止docker容器退出
CMD /usr/sbin/sshd && tail -f /var/log/wtmp
一、构建镜像
Dockerfile目录下执行,yeah,就是chenqionghe/centos镜像,你也可以弄成自己的,例如muscle/lightwegiht
1 |
|
二、运行容器
1 |
|
三、使用ssh连接容器
这里使用了2222端口来映射容器里的22端口,运行起来就可以使用ssh连接了,输出设置好的abcd1234密码,注意,这里用的是2222映射的端口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
以上就是使用docker开启ssh模拟服务器的全过程,ubuntu应该也差不多,感举的小伙伴可以试试
hight起来,light weight baby!