1、进入docker中的linux须要装置ssh服务

apt-get updateapt-get install openssh-server openssh-client

2、设置docker的root登录权限为yes

批改/etc/ssh/sshd_config文件,找到PermitRootLogin,参数批改为yes

vim /etc/ssh/sshd_config

3、批改docker的root明码

应用passwd root命令批改root登录明码

passwd root

4、确认docker中密匙是否存在,没有则须要创立

如下是不存在密匙

root@c223a9d1c7a0:/home# ll /etc/sshtotal 560drwxr-xr-x 1 root root   4096 Oct 10 03:29 ./drwxr-xr-x 1 root root   4096 Oct 10 03:32 ../-rw-r--r-- 1 root root 553122 Feb 10  2018 moduli-rw-r--r-- 1 root root   1580 Feb 10  2018 ssh_config-rw-r--r-- 1 root root   3800 Oct 10 03:29 sshd_config

生成主秘密匙(执行如下命令时能够一路回车):

  1. 生成rsa_key (-t示意生成的密钥所应用的加密类型;-f项后接要生成的密钥文件名)
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
  1. 生成ecdsa_key
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
  1. 生成ed25519_key。
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

5、退出容器

exit命令退出容器

6、提交这个container为一个新的image

docker commit <容器id> <镜像名称>

7、应用新的镜像启动一个指定了ip映射的新容器

把容器的22端口映射到虚拟机的8122端口

docker run --name <容器名称> -i -t -p 8122:22 <镜像ID> /usr/sbin/sshd -D

8、在主机ssh登录docker

ssh root@localhost -p 8122

一生学习!