写dockerfile
[root@web01 sshddockerfile]# cat Dockerfile FROM centosMAINTAINER liuhaixiaoRUN yum -y install wget RUN rm -rf /etc/yum.repos.d/* RUN wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo RUN yum clean allRUN yum install -y openssh-server sudoRUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_configRUN useradd admin RUN echo "admin:admin" | chpasswd RUN echo "admin ALL=(ALL) ALL" >> /etc/sudoers RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key # 启动sshd服务并且暴露22端口 RUN mkdir /var/run/sshd EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"][root@web01 sshddockerfile]#
2.创建镜像
docker build -t sshddocker ./
3.查看镜像
[root@web01 sshddockerfile]# docker p_w_picpathsREPOSITORY TAG IMAGE ID CREATED SIZEsshddocker latest 73204ab652dd 12 minutes ago 323.2 MB
4.根据此镜像启动一个容器
docker run -d --name mysshdcontainer IP:23:22 sshddocker
5.查看生成的容器
[root@web01 sshddockerfile]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf4620f5dd045 sshddocker "/usr/sbin/sshd -D" 6 minutes ago Up 6 minutes IP:23->22/tcp mysshdcontainer[root@web01 sshddockerfile]#
6.远程登录测试
[root@web02 ~]# ssh admin@59.110.25.215 -p 23admin@59.110.25.215's password: [admin@f4620f5dd045 ~]$ [admin@f4620f5dd045 ~]$ sudo -i [sudo] password for admin: [root@f4620f5dd045 ~]# pwd/root
7.查看容器的IP
yum -y install net-tools[root@f4620f5dd045 ~]# ifconfigeth0: flags=4163mtu 1500 inet 192.168.0.2 netmask 255.255.240.0 broadcast 0.0.0.0 inet6 fe80::42:c0ff:fea8:2 prefixlen 64 scopeid 0x20 ether 02:42:c0:a8:00:02 txqueuelen 0 (Ethernet) RX packets 814 bytes 377183 (368.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 565 bytes 64761 (63.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 0 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@f4620f5dd045 ~]#