1.Dockerfile文件
FROM registry.aliyuncs.com/acs-sample/centos:6
MAINTAINER xuqh "xqh_163@163.com"
RUN yum install -y openssh-server
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN echo 'root:TopDocker' |chpasswd
RUN sed -ri 's/^PermitRootLogins+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
EXPOSE 22
EXPOSE 80
CMD ["/usr/sbin/sshd", "-D"]
2.创建镜像然后查看
docker build -t aliyun/centos-sshd:6 .
[root@iZ23g3sxtunZ dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
aliyun/centos-sshd 6 a8eea9913432 17 minutes ago 277.5 MB
registry.aliyuncs.com/acs-sample/centos 6 63b04d6634cc 4 weeks ago 190.6 MB
3.启动容器然后查看
docker run -d -p 10022:22 -p 10080:80 --name centos aliyun/centos-sshd:6
[root@iZ23g3sxtunZ dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e188fc592756 aliyun/centos-sshd:6 "/usr/sbin/sshd -D" 19 minutes ago Up 19 minutes 0.0.0.0:10022->22/tcp, 0.0.0.0:10080->80/tcp centos
注:可以用netstat -lntp查看端口信息
4.宿主机上登陆测试
ssh root@192.168.x.x -p 10022
5.如果ssh提示IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY
rm -rf ~/.ssh/known_hosts