建立可以ssh的docker容器

來源:互聯網
上載者:User

標籤:docker ssh

作業系統:vm centos6.5 x64 192.168.240.148

docker容器:centos6.5 x64 172.17.0.3


1.下載原始鏡像


[[email protected] ~]#docker pull centos ###一般會拉最新版centos7和centos6.5過來,這裡使用的是centos6.5###


2.安裝openssh

[[email protected] ~]# docker run -i -t kw1073/centos-new4 /bin/bashbash-4.1#yum -y install openssh-server openssh-clientsbash-4.1#chkconfig sshd onbash-4.1#service sshd start######顯示如下#####
Generating SSH1 RSA host key:                              [  OK  ]Generating SSH2 RSA host key:                              [  OK  ]Generating SSH2 DSA host key:                              [  OK  ]Starting sshd:                                             [  OK  ]


3.ssh到宿主機中

bash-4.1#ssh 192.168.240.148 -p61618[email protected]‘s password: Last login: Thu Jul 31 22:39:38 2014 from 192.168.240.1[[email protected] ~]#


4.安裝passwd

bash-4.1#yum install passwd ###docker官網上的centos6.5原始鏡像中沒有passwd,用於修改root使用者的密碼###bash-4.1#passwd rootbash-4.1#useradd kw1073


5.使用commit命令儲存鏡像

[[email protected] ~]#docker ps -l  ###查看docker的id###

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/43/5D/wKiom1PZ7TqiK8bWAADDsE7t8k0901.jpg" title="1.png" alt="wKiom1PZ7TqiK8bWAADDsE7t8k0901.jpg" />

[[email protected] ~]#docker commit 6b84dba2a9bb kw1073/centos-new4 ###做成可ssh的docker鏡像###


6.在宿主機上映射連接埠

[[email protected] ~]#docker run -u -t -p 127.0.0.1:222:22 kw1073/centos-new4  /bin/bash ###做好docker鏡像和宿主機的連接埠映射,這裡會直接進入bash環境###


7.在docker容器中啟動ssh服務

bash-4.1#service sshd startbash-4.1#ifconfig
eth0 Link encap:Ethernet HWaddr 82:E2:4F:F5:12:E9          inet addr:172.17.0.3 Bcast:0.0.0.0 Mask:255.255.0.0          inet6 addr: fe80::80e2:4fff:fef5:12e9/64 Scope:Link          UP BROADCAST RUNNING MTU:1500 Metric:1          RX packets:4882 errors:0 dropped:0 overruns:0 frame:0          TX packets:4107 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000          RX bytes:27592541 (26.3 MiB) TX bytes:342979 (334.9 KiB)


8.在宿主機上進行ssh串連

[[email protected] ~]#ssh 172.0.0.3The authenticity of host ‘172.17.0.3 (172.17.0.3)‘ can‘t be established.RSA key fingerprint is 40:4f:f2:9a:55:07:9e:a0:cb:42:60:4f:92:fa:90:e7.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added ‘172.17.0.3‘ (RSA) to the list of known hosts.[email protected]‘s password: Last login: Thu Jul 31 10:26:24 2014 from 172.17.42.1Connection to 172.17.0.3 closed.


9.修改容器中ssh的配置

####從上一步可以看到,ssh串連錯誤,可以登入,但是一登入上去就馬上斷開####
bash-4.1# sed -i ‘s/UsePAM yes/UsePAM no/‘ /etc/ssh/sshd_config   #修改sshd設定檔bash-4.1# /etc/init.d/sshd reload bash-4.1# /etc/init.d/sshd restart                    #重啟sshd服務Stopping sshd:                                             [  OK  ]Starting sshd:                                             [  OK  ]


10.在宿主機上重試一次ssh串連

[[email protected] dist]# ssh 172.17.0.3 [email protected]‘s password: Last login: Thu Jul 31 11:48:51 2014 from 172.17.42.1 -bash-4.1# ls或者如下串連:[[email protected] dist]# ssh [email protected] -p222 [email protected]‘s password: Last login: Thu Jul 31 15:36:56 2014 from 172.17.42.1#####因為做了連接埠映射,所以可以直接從映射的連接埠登陸###


11.設定docker容器中的環境變數

#######直接從宿主機上拷貝一份過去########[[email protected] dist]# scp /root/.bash* 172.17.0.3:/root/


12.在宿主機上重新ssh到容器中

[[email protected] dist]# ssh 172.17.0.3 [email protected]‘s password: Last login: Thu Jul 31 15:07:00 2014 from 172.17.42.1 [[email protected] ~]#


13.儲存配置好的docker容器為鏡像

#######查看當前運行中的docker容器#######[[email protected] ~]# docker ps -a

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/43/5E/wKioL1PZ8WGQEfVEAAFkJJ6Wx5s434.jpg" title="2.png" alt="wKioL1PZ8WGQEfVEAAFkJJ6Wx5s434.jpg" />

#######儲存容器為鏡像#######[[email protected] ~]# docker commit 89741e6f8e61 kw1073/centos-new517a270866d48429d582aae50b6f3183a4e76c5b4813b1f6e96b5684cb222b100
#####查看當前的鏡像########[[email protected] ~]# docker images

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/43/5E/wKioL1PZ8oeiGVA1AAKPBPYmO90349.jpg" title="3.png" alt="wKioL1PZ8oeiGVA1AAKPBPYmO90349.jpg" />

本文出自 “營運小卒” 部落格,謝絕轉載!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.