Docker-创建支持ssh服务的镜像

时间:2024-10-12 08:34:08

这里测试tomcat镜像安装ssh服务

1.启动镜像

[root@wls12c docker]$ docker run -d  tomcat:centos
844bdde121a03174f3abd226a9ef286938b6ea4a1fbd2659961b1e8542ca3ccd
[root@wls12c docker]$ docker exec -it /bin/bash
[root@844bdde121a0 /]#

2.安装ssh服务

[root@844bdde121a0 /]# yum -y install openssh-server

3.要正常启动服务必须存在"/var/run/sshd"目录,创建它

[root@844bdde121a0 /]# mkdir /var/run/sshd

4.启动sshd服务

[root@844bdde121a0 /]# /usr/sbin/sshd -D &
[]

5.查看启动端口

[root@844bdde121a0 /]# lsof -i:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd root 3u IPv4 0t0 TCP *:ssh (LISTEN)
sshd root 4u IPv6 0t0 TCP *:ssh (LISTEN)

6.将宿主机的公钥导入到/root/.ssh/authorized_keys目录下

[root@844bdde121a0 /]# mkdir root/.ssh
[root@844bdde121a0 /]# vi /root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApcDYVQ7JdPYwyWzdna/T2Z41wRypL+zJQt0zA3hbf6kZMI6gmPTsDl1t37swyj
xtlLK1Saiq5rE12gL7qfIsfR5Vwunmj4uKXfNBjSh472GXJOpHPB+Wz28Xf53Yc0x589YSdkE3lsJ8EFpdQntuETj4q47905ji
ZEjaukAp+uy6qNLTXaaTLUzod4Y8t9h5KbqYKfE+wfnVrfpSJhzVTLPSJ7NiBtQTccMCQkxeNYgtaBPLh7I3OmA1hsoDuRbDcX
9RivGLkrdct0WT7W/IFPhdF2vHZ6QRTvYw3B6//RYFK7/nkRJglvsERyPJdUCU340xNiplCepR3yaxSMSmpw== root@wls12c

7.编辑ssh服务可执行文件

[root@844bdde121a0 /]# vi run.sh
#/bin/bash
/usr/sbin/sshd -D &

8.给run.sh可执行权限

chmod  /run.sh

9.查看是否生成了主机密钥,没有则生成

[root@91f4be81e455 ssh]# rm -rf ssh*key
[root@91f4be81e455 ssh]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
[root@91f4be81e455 ssh]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
[root@91f4be81e455 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 : -p :  /run.sh

12.可以在宿主机和其它机器进行连接了

root@wls12c ~]$ ssh 192.168.0.80 -p
The authenticity of host '[192.168.0.80]:2222 ([192.168.0.80]:2222)' can't be established.
RSA key fingerprint is ::c4:ab::ea:7e::3b:3d:::db::a3:.
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.
[root@04cbe75bfef9 ~]#

相关文章