Docker ssh远程连接使用


查看docker镜像

[root@xiaoniu ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
insaneworks/centos   latest              863eb06e0f75        5 months ago         126.6 MB
learn/tutorial       latest              8dbd9e392a96        2.283295 years ago   128 MB

交互方式创建容器

[root@xiaoniu ~]# docker run -i -t insaneworks/centos /bin/bash
bash-4.1# yum install openssh-server -y 
bash-4.1# service sshd start
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]
bash-4.1# yum install passwd -y

问题

bash-4.1# passwd root
Changing password for user root.
New password: 
/usr/share/cracklib/pw_dict.pwd: No such file or directory
PWOpen: No such file or directory

解决:

bash-4.1# yum reinstall cracklib-dicts -y


设置root密码

bash-4.1# passwd root
Changing password for user root.
New password: 
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.

修改sshd_config

bash-4.1# yum install vi -y
bash-4.1# vi /etc/ssh/sshd_config
        UsePAM yes改成UsePAM no
bash-4.1# service sshd restart 


保存当前状态到新镜像

[root@xiaoniu ~]# docker ps
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS               NAMES
accefb562a4d        insaneworks/centos:latest   "/bin/bash"         8 minutes ago       Up 8 minutes                            romantic_meitner    
[root@xiaoniu ~]# docker commit accefb562a4d llsshd
d277570a7ccc39d6d99ed37f11fb6bed31935e01a5d937bc6ee7d7c32075596b
[root@xiaoniu ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS                     PORTS               NAMES
accefb562a4d        insaneworks/centos:latest   "/bin/bash"         9 minutes ago       Exited (0) 5 seconds ago                       romantic_meitner    
[root@xiaoniu ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
llsshd               latest              d277570a7ccc        29 seconds ago       195 MB
insaneworks/centos   latest              863eb06e0f75        5 months ago         126.6 MB
learn/tutorial       latest              8dbd9e392a96        2.283313 years ago   128 MB
[root@xiaoniu ~]# docker run -d -p 22 llsshd /usr/sbin/sshd -D
5c16ef8bf156b6b5b51de673650b5c32109d700ea90ad2028e833eb6dfbfa1c7
[root@xiaoniu ~]# docker port 5c1 22
0.0.0.0:49154

连接测试

[root@xiaoniu ~]# ssh root@127.0.0.1 -p 49154
The authenticity of host '[127.0.0.1]:49154 ([127.0.0.1]:49154)' can't be established.
RSA key fingerprint is 0b:bc:6a:be:ca:bb:ca:b4:c9:be:3c:26:87:ea:fe:a0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:49154' (RSA) to the list of known hosts.
root@127.0.0.1's password: 
-bash-4.1# ifconfig
eth0      Link encap:Ethernet  HWaddr C2:53:8B:04:33:65  
          inet addr:172.17.0.5  Bcast:172.17.255.255  Mask:255.255.0.0
          inet6 addr: fe80::c053:8bff:fe04:3365/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:41593 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36603 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:51819285 (49.4 MiB)  TX bytes:5398781 (5.1 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

-bash-4.1# exit


另附一篇好文章(关于容器中真的需要sshd吗):http://www.server110.com/docker/201411/11216.html


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐