写dockerfile
[root@web01 sshddockerfile]# cat Dockerfile FROM centos MAINTAINER liuhaixiao RUN 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 all RUN yum install -y openssh-server sudo RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config RUN 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_picpaths REPOSITORY TAG IMAGE ID CREATED SIZE sshddocker 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 NAMES f4620f5dd045 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 23 admin@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 ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 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<link> 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 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> 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 ~]#
转载于:https://blog.51cto.com/wsxxsl/1889535