Docker ssh遠程連接使用

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 [email protected] -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.
[email protected]'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


發佈了59 篇原創文章 · 獲贊 7 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章