示例 - CentOS7
[root@CentOS-7 ~]# cat ssh-centos7 FROM centos:centos7MAINTAINER anliven "anliven@yeah.net" ENV http_proxy="http://10.144.1.10:8080"RUN yum install -y openssh-server \ && yum install -y inetutils-ping iproute net-tools \ && yum clean all \ && echo '123456' | passwd --stdin root \ && ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key \ && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key EXPOSE 22CMD ["/usr/sbin/sshd", "-D"][root@CentOS-7 ~]# [root@CentOS-7 ~]# docker build --file ssh-centos7 --tag ssh:centos7 .Sending build context to Docker daemon 18.55 MBStep 1 : FROM centos:centos7 ---> a8493f5f50ffStep 2 : MAINTAINER anliven "anliven@yeah.net" ---> Running in 204f723fabdd ---> 89fa55e17d5fRemoving intermediate container 204f723fabddStep 3 : ENV http_proxy "http://10.144.1.10:8080" ---> Running in 2f70b94e0d63 ---> 0e50b2cfdc9eRemoving intermediate container 2f70b94e0d63Step 4 : RUN yum install -y openssh-server && yum install -y inetutils-ping iproute net-tools && yum clean all && echo '123456' | passwd --stdin root && ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key ---> Running in fb4b2380f5ec.................. ---> d1548e06d027Removing intermediate container fb4b2380f5ecStep 5 : EXPOSE 22 ---> Running in 03afbb7ffc45 ---> 90c69174857bRemoving intermediate container 03afbb7ffc45Step 6 : CMD /usr/sbin/sshd -D ---> Running in a07326dc62aa ---> 3bf6495f5de6Removing intermediate container a07326dc62aaSuccessfully built 3bf6495f5de6[root@CentOS-7 ~]# [root@CentOS-7 ~]# docker images ssh:centos7REPOSITORY TAG IMAGE ID CREATED SIZEssh centos7 3bf6495f5de6 About a minute ago 217.9 MB[root@CentOS-7 ~]# [root@CentOS-7 ~]# docker run -d -P ssh:centos71733db18cfd18735504272f90655d832b36e898998e5054bbbf499fcab381827[root@CentOS-7 ~]# [root@CentOS-7 ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1733db18cfd1 ssh:centos7 "/usr/sbin/sshd -D" 7 seconds ago Up 6 seconds 0.0.0.0:32769->22/tcp amazing_hamilton[root@CentOS-7 ~]# [root@CentOS-7 ~]# ssh -p 32769 root@0.0.0.0The authenticity of host '[0.0.0.0]:32769 ([0.0.0.0]:32769)' can't be established.RSA key fingerprint is cf:28:2c:32:94:82:40:2a:b3:54:51:6f:f6:0b:43:e8.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '[0.0.0.0]:32769' (RSA) to the list of known hosts.root@0.0.0.0's password: [root@1733db18cfd1 ~]# [root@1733db18cfd1 ~]# ip addr show |grep eth052: eth0@if53: mtu 1500 qdisc noqueue state UP inet 172.17.0.2/16 scope global eth0[root@1733db18cfd1 ~]# [root@1733db18cfd1 ~]# exitlogoutConnection to 0.0.0.0 closed.[root@CentOS-7 ~]#
示例 - Ubuntu16.04
[root@CentOS-7 ~]# cat ssh-ubuntu16 FROM ubuntu:16.04MAINTAINER anliven "anliven@yeah.net"ENV http_proxy="http://10.144.1.10:8080"RUN apt-get update \ && apt-get install -y openssh-server \ && apt-get install -y inetutils-ping iproute net-tools \ && apt-get clean \ && mkdir /var/run/sshd \ && sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config \ && echo "root:123456" | chpasswdEXPOSE 22CMD ["/usr/sbin/sshd", "-D"][root@CentOS-7 ~]# [root@CentOS-7 ~]# docker build --file ssh-ubuntu16 --tag ssh:ubuntu16 .Sending build context to Docker daemon 18.55 MBStep 1 : FROM ubuntu:16.04 ---> f7b3f317ec73Step 2 : MAINTAINER anliven "anliven@yeah.net" ---> Running in 6259dee07753 ---> 427750397620Removing intermediate container 6259dee07753Step 3 : ENV http_proxy "http://10.144.1.10:8080" ---> Running in bcb0bd688544 ---> aa313abb23c8Removing intermediate container bcb0bd688544Step 4 : RUN apt-get update && apt-get install -y openssh-server && apt-get install -y inetutils-ping iproute net-tools && apt-get clean && mkdir /var/run/sshd && sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config && echo "root:123456" | chpasswd ---> Running in a916fc8fc699.................. ---> fee74bafa989Removing intermediate container a916fc8fc699Step 5 : EXPOSE 22 ---> Running in c6d1032bd372 ---> a99434e93cc6Removing intermediate container c6d1032bd372Step 6 : CMD /usr/sbin/sshd -D ---> Running in 4750dcb341a6 ---> c5cbd98fb8c7Removing intermediate container 4750dcb341a6Successfully built c5cbd98fb8c7[root@CentOS-7 ~]# [root@CentOS-7 ~]# docker images ssh:ubuntu16REPOSITORY TAG IMAGE ID CREATED SIZEssh ubuntu16 c5cbd98fb8c7 47 seconds ago 215.6 MB[root@CentOS-7 ~]# [root@CentOS-7 ~]# docker run -d -P ssh:ubuntu16f92e3274421f3b0dfca0605bc0c8df3ea13dcace3a307e77f4d600f819645537[root@CentOS-7 ~]# [root@CentOS-7 ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf92e3274421f ssh:ubuntu16 "/usr/sbin/sshd -D" 5 seconds ago Up 4 seconds 0.0.0.0:32773->22/tcp dreamy_brahmagupta[root@CentOS-7 ~]# [root@CentOS-7 ~]# ssh -p 32773 root@0.0.0.0The authenticity of host '[0.0.0.0]:32773 ([0.0.0.0]:32773)' can't be established.ECDSA key fingerprint is 01:66:ff:54:44:34:92:ab:6d:7a:77:a9:6a:18:7d:1d.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '[0.0.0.0]:32773' (ECDSA) to the list of known hosts.root@0.0.0.0's password: Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 3.10.0-327.el7.x86_64 x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantageThe programs included with the Ubuntu system are free software;the exact distribution terms for each program are described in theindividual files in /usr/share/doc/*/copyright.Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted byapplicable law.root@f92e3274421f:~# root@f92e3274421f:~# exitlogoutConnection to 0.0.0.0 closed.[root@CentOS-7 ~]#