Docker-创建支持ssh服务的镜像
Posted 头痛不头痛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker-创建支持ssh服务的镜像相关的知识,希望对你有一定的参考价值。
这里测试tomcat镜像安装ssh服务
1.启动镜像
[[email protected] docker]$ docker run -d tomcat:centos 844bdde121a03174f3abd226a9ef286938b6ea4a1fbd2659961b1e8542ca3ccd [[email protected] docker]$ docker exec -it 844 /bin/bash [[email protected] /]#
2.安装ssh服务
[[email protected] /]# yum -y install openssh-server
3.要正常启动服务必须存在"/var/run/sshd"目录,创建它
[[email protected] /]# mkdir /var/run/sshd
4.启动sshd服务
[[email protected] /]# /usr/sbin/sshd -D & [1] 118
5.查看启动端口
[[email protected] /]# lsof -i:22 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 118 root 3u IPv4 207270 0t0 TCP *:ssh (LISTEN) sshd 118 root 4u IPv6 207272 0t0 TCP *:ssh (LISTEN)
6.将宿主机的公钥导入到/root/.ssh/authorized_keys目录下
[[email protected] /]# mkdir root/.ssh [[email protected] /]# vi /root/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApcDYVQ7JdPYwyWzdna/T2Z41wRypL+zJQt0zA3hbf6kZMI6gmPTsDl1t37swyj xtlLK1Saiq5rE12gL7qfIsfR5Vwunmj4uKXfNBjSh472GXJOphpB+Wz28Xf53Yc0x589YSdkE3lsJ8EFpdQntuETj4q47905ji ZEjaukAp+uy6qNLTXaaTLUzod4Y8t9h5KbqYKfE+wfnVrfpSJhzVTLPSJ7NiBtQTccMCQkxeNYgtaBPLh7I3OmA1hsoDuRbDcX 9RivGLkrdct0WT7W/IFPhdF2vHZ6QRTvYw3B6//RYFK7/nkRJglvsERyPJdUCU340xNiplCepR3yaxSMSmpw== [email protected]
7.编辑ssh服务可执行文件
[[email protected] /]# vi run.sh #/bin/bash /usr/sbin/sshd -D &
8.给run.sh可执行权限
chmod 755 /run.sh
9.查看是否生成了主机密钥,没有则生成
[[email protected] ssh]# rm -rf ssh*key [[email protected] ssh]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key [[email protected] ssh]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key [[email protected] ssh]# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
10.保存镜像
docker commit 91f tomcat-sshd
11.运行镜像
docker run --name tomcat--ssh -d -p 100022:22 -p 8090:8080 3364 /run.sh
12.可以在宿主机和其它机器进行连接了
[email protected] ~]$ ssh 192.168.0.80 -p 10022 The authenticity of host ‘[192.168.0.80]:2222 ([192.168.0.80]:2222)‘ can‘t be established. RSA key fingerprint is 03:15:c4:ab:97:ea:7e:58:3b:3d:22:42:db:50:a3:46. Are you sure you want to continue connecting (yes/no)? yesy Warning: Permanently added ‘[192.168.0.80]:2222‘ (RSA) to the list of known hosts. [[email protected] ~]#
以上是关于Docker-创建支持ssh服务的镜像的主要内容,如果未能解决你的问题,请参考以下文章
通过Dockerfile创建支持SSH服务的docker镜像(centosUbuntu)