RUN cp /root/sources.list /etc/apt/sources.list.d/aliyun.list
RUN apt-get update
RUN apt-get install -y openssh-server
RUN apt-get install -y net-tools
RUN apt-get install -y vim
RUN mkdir -p /var/run/sshd
RUN mkdir -p mkdir/root/.ssh/
产生公私钥
[root@a3c8baf6961e /]# ssh-keygen -t rsa //一路回车
[root@a3c8baf6961e /]# cd ~/.ssh/
[root@a3c8baf6961e .ssh]# ls
id_rsa id_rsa.pub
[root@a3c8baf6961e .ssh]# cp id_rsa.pub authorized_keys
[root@a3c8baf6961e .ssh]# ls
authorized_keys id_rsa id_rsa.pub
执行sshd命令,有报错:
[root@a3c8baf6961e .ssh]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_dsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af40bd07fa0f centos7:7.3.1611 "/sbin/init" 21 minutes ago Up 21 minutes nauseous_shirley
记住这个容器ID,然后关闭
[root@localhost ~]#docker stop af40bd07fa0f
af40bd07fa0f
接着提交改为新的镜像,使用上一步的容器ID,提交名为wangssh的镜像(提交成功后,之前创建的容器可以选择删除(docker ps -a 查看);当然不删除也不影响。
建议不要删除,可以再次启用提交新的镜像以便他用。)
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a7c1406361e wangssh "/usr/sbin/sshd -D" About a minute ago Up About a minute 0.0.0.0:220->22/tcp focused_hawking
[root@aea267757cc9 /]# cd ~
[root@aea267757cc9 ~]# cd .ssh/
[root@aea267757cc9 .ssh]# cp id_rsa.pub authorized_keys
接着提交为新镜像
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aea267757cc9 centos7:7.3.1611 "/sbin/init" About an hour ago Up 38 minutes admiring_hodgkin
fc726a6a27d2 centos "/bin/bash" 3 months ago Up 3 months 0.0.0.0:32772->80/tcp web1
9d99c7b9451b centos "/bin/bash" 3 months ago Up 3 months 0.0.0.0:32769->8080/tcp web3
[root@localhost ~]#docker stop aea267757cc9
aea267757cc9
Last login: Mon Mar 13 10:03:54 2017
---------------------------------------------------------------------------------------------------------------------
当登陆到容器后,可以查看下容器ip
第一种方式:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b220fabf815a wangssh "/usr/sbin/sshd -D" 6 hours ago Up About an hour 0.0.0.0:20020->22/tcp gigantic_goldwasser
fc726a6a27d2 980e0e4c79ec "/bin/bash" 3 months ago Up About an hour 0.0.0.0:32768->80/tcp web1
9d99c7b9451b 980e0e4c79ec "/bin/bash" 3 months ago Up About an hour 0.0.0.0:32769->8080/tcp web3
[root@ b5926410fe60 /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4122f818a741 herong/centos7-ssh:latest "/usr/sbin/sshd" 13 seconds ago Exited (0) 13 seconds ago happy_mclean
hr:centos7 hr$ docker rm -f 4122f818a741
--基于新镜像运行容器
hr:centos7 hr$ docker run -d -p 10022:22 herong/centos7-ssh:latest /usr/sbin/sshd -D
--查看映射端口是否成功
hr:centos7 hr$ docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4966d35fe0a3 herong/centos7-ssh:latest "/usr/sbin/sshd -D" 3 seconds ago Up 3 seconds 0.0.0.0:10022->22/tcp compassionate_kowalevski
hr:centos7 hr$ docker port 4966d35fe0a3
22/tcp -> 0.0.0.0:10022
七. 从宿主机连接到容器
w 如果宿主机是非linux操作系统,则需要通过docker-machine ip连到容器
-- 查看docker-machine Ip地址
hr:centos7 hr$ docker-machine ip default
192.168.99.100
--通过docker-machine ip 连接到容器,输入之前设置的密码即可登录成功
hr:centos7 hr$ ssh root@192.168.99.100 -p 10022
The authenticity of host '[192.168.99.100]:10022 ([192.168.99.100]:10022)' can't be established.
ECDSA key fingerprint is SHA256:d3JNckcTVv1ASJlwv+IT/bJwlzMC4U1T/PmsKYIHMhQ.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.99.100]:10022' (ECDSA) to the list of known hosts.
root@192.168.99.100's password: