前面幾篇已經介紹了Docker基礎環境的部署,下面介紹下通過ssh方式登陸Docker容器的操作記錄(其實不太建議直接用ssh去連線上容器的想法,雖然可以,但是有很多弊端,而且docker已經提供了容器內執行的命令,沒有必要再折騰每一個容器為sshd伺服器。具體參考:http://jpetazzo.github.io/2014/06/23/docker-ssh-considered-evil/)
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7 7.3.1611 d5ebea14da54 2 weeks ago 311 MB <none> <none> d5c154b612c8 2 weeks ago 311 MB test latest ecefde07358f 11 weeks ago 599.6 MB learn/ping latest fea07d84b0df 4 months ago 196.7 MB docker.io/tomcat latest ebb17717bed4 4 months ago 355.4 MB docker.io/centos latest 980e0e4c79ec 6 months ago 196.7 MB nginx 1.9 c8c29d842c09 9 months ago 182.7 MB docker.io/redis 2.8.19 dd9fe7db5236 22 months ago 110.7 MB [root@localhost ~]# docker run -i -t centos7:7.3.1611 /bin/bash [root@a3c8baf6961e /]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) [root@a3c8baf6961e /]# yum install wget vim [root@a3c8baf6961e /]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 安裝ssh服務端 [root@a3c8baf6961e /]# yum cleal all [root@a3c8baf6961e /]# yum install passwd [root@a3c8baf6961e /]# yum install openssh-server 修改容器密碼(提前yum -y reinstall cracklib-dicts) [root@a3c8baf6961e /]# echo "123456" |passwd --stdin root 產生公私鑰 [root@a3c8baf6961e /]# ssh-keygen -t rsa //一路回車 [root@a3c8baf6961e /]# cd ~/.ssh/ [root@a3c8baf6961e .ssh]# ls id_rsa id_rsa.pub [root@a3c8baf6961e .ssh]# cp id_rsa.pub authorized_keys [root@a3c8baf6961e .ssh]# ls authorized_keys id_rsa id_rsa.pub 執行sshd命令,有報錯: [root@a3c8baf6961e .ssh]# /usr/sbin/sshd Could not load host key: /etc/ssh/ssh_host_rsa_key Could not load host key: /etc/ssh/ssh_host_dsa_key Could not load host key: /etc/ssh/ssh_host_ecdsa_key Could not load host key: /etc/ssh/ssh_host_ed25519_key 解決辦法: [root@a3c8baf6961e .ssh]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key //均是一路回車 [root@a3c8baf6961e .ssh]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key [root@a3c8baf6961e .ssh]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key [root@a3c8baf6961e .ssh]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key 再次執行sshd命令,如果沒有報錯,說明可以啟動了 [root@a3c8baf6961e .ssh]# /usr/sbin/sshd [root@a3c8baf6961e .ssh]# -----------------------啟動ssh,如果報錯如下(這是centos7下的一個bug)------------------------- [root@a3c8baf6961e .ssh]# systemctl restart sshd.service Failed to get D-Bus connection: Operation not permitted 這個報錯在之前的文件裡就已經提到過了 解決辦法如下: 先把上面的容器關閉(docker stop container-id),然後重新啟動容器,啟動時加上引數--privileged(特權引數,也可以是--privileged=true,如果啟動容器中掛載目錄沒有許可權也可以新增此引數)和/sbin/init(代替/bin/bash),如下: [root@localhost ~]# docker run --privileged -i -t centos7:7.3.1611 /sbin/init 上面的容器啟動後,會一直在卡著的狀態中,先不用管,開啟另一個終端視窗,檢視容器 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES af40bd07fa0f centos7:7.3.1611 "/sbin/init" 28 seconds ago Up 28 seconds nauseous_shirley 然後按照容器的ID進去,這個時候再根據/bin/bash進入容器(前面加exec -it引數),接著重啟ssh服務就ok了 [root@localhost ~]# docker exec -it af40bd07fa0f /bin/bash [root@af40bd07fa0f /]# systemctl restart sshd.service [root@af40bd07fa0f /]# echo "123456" |passwd --stdin root //注意這裡由於上述特殊情況重新啟動了容器,之前建立的root密碼無效了(這就相當於重新另起了一個容器),需要重新修改下root密碼!!可以隨便建立個使用者,然後切換到root,測試下之前建立的root密碼是否還有效! -------------------------------------------------------------------------------------------------- 檢視ssh埠,發現22埠已經開啟 [root@af40bd07fa0f /]# ss -a|grep ssh tcp LISTEN 0 128 *:ssh *:* tcp LISTEN 0 128 :::ssh :::* [root@af40bd07fa0f /]# ss -ln|grep 22 u_dgr UNCONN 0 0 * 26884224 * 26885412 tcp LISTEN 0 128 *:22 *:* tcp LISTEN 0 128 :::22 :::*
然後docker ps檢視下容器,提交更改為新映象,執行新的映象
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES af40bd07fa0f centos7:7.3.1611 "/sbin/init" 21 minutes ago Up 21 minutes nauseous_shirley 記住這個容器ID,然後關閉 [root@localhost ~]# docker stop af40bd07fa0f af40bd07fa0f 接著提交改為新的映象,使用上一步的容器ID,提交名為wangssh的映象(提交成功後,之前建立的容器可以選擇刪除(docker ps -a 檢視);當然不刪除也不影響。建議不要刪除,可以再次啟用提交新的映象以便他用。) [root@localhost ~]# docker commit af40bd07fa0f wangssh sha256:ca5e393b7605949e58c1067c1bc73d99d52f47107756f0ade1725ca04886fd71 [root@localhost ~]# 提交成功後,使用docker images可以檢視到 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE wangssh latest ca5e393b7605 57 seconds ago 327.1 MB centos7 7.3.1611 d5ebea14da54 2 weeks ago 311 MB 然後執行新的映象 [root@localhost ~]# docker run -d -p 220:22 wangssh /usr/sbin/sshd -D b0a845a3dedeac7b46002d1c8514077309d88dcc0667b7080bc1ab67d70eb167 docker: Error response from daemon: Cannot start container b0a845a3dedeac7b46002d1c8514077309d88dcc0667b7080bc1ab67d70eb167: [9] System error: SELinux policy denies access.. 如上出現上面的報錯,這是由於selinux造成的!需要關閉selinux,如下: [root@localhost ~]# setenforce 0 [root@localhost ~]# getenforce Permissive 然後再次執行新的映象,就成功了! [root@localhost ~]# docker run -d -p 220:22 wangssh /usr/sbin/sshd -D 0a7c1406361ef52dcc5c32801e4c7c231078594cd7010375ea33fe3024cc9126 [root@localhost ~]# 上面執行命令中的引數解釋: -d 後臺執行容器 -p 容器埠對映到主機[可選] 使用docker ps檢視執行的容器 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a7c1406361e wangssh "/usr/sbin/sshd -D" About a minute ago Up About a minute 0.0.0.0:220->22/tcp focused_hawking 此時你可以直接連線容器,也可以通過埠對映連線容器(使用之前建立的容器密碼123456登陸) [root@localhost ~]# ssh -p220 root@localhost root@localhost's password: [root@0a7c1406361e ~]#
------------------------------------------------------------------------------------------------------------------
如果要想做ssh無密碼登陸的信任關係,只需要將物理機本地的~/.ssh/id_rsa.pub拷貝到容器裡的~/.ssh/authorized_keys即可
接著上面ID為aea267757cc9的容器登陸後的操作: [root@localhost ~]# docker exec -it aea267757cc9 /bin/bash [root@aea267757cc9 /]# ssh-keygen -t rsa //一路回車 將物理機本地的~/.ssh/id_rsa.pub拷貝到容器裡 [root@localhost ~]# docker cp ~/.ssh/id_rsa.pub aea267757cc9:/root/.ssh/ 然後到容器裡將id_rsa.pub拷貝為authorized_keys [root@aea267757cc9 /]# cd ~ [root@aea267757cc9 ~]# cd .ssh/ [root@aea267757cc9 .ssh]# cp id_rsa.pub authorized_keys 接著提交為新映象 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES aea267757cc9 centos7:7.3.1611 "/sbin/init" About an hour ago Up 38 minutes admiring_hodgkin fc726a6a27d2 centos "/bin/bash" 3 months ago Up 3 months 0.0.0.0:32772->80/tcp web1 9d99c7b9451b centos "/bin/bash" 3 months ago Up 3 months 0.0.0.0:32769->8080/tcp web3 [root@localhost ~]# docker stop aea267757cc9 aea267757cc9 [root@localhost ~]# docker commit aea267757cc9 hahassh sha256:906bf1bd2a156b1222def7d3d21fbc2cd7e963fc923f5a6da92e6b45954688d9 [root@localhost ~]# setenforce 0 [root@localhost ~]# docker run -d -p 220:22 hahassh /usr/sbin/sshd -D 8b9c153463c73122cfd787a27190a8665f54fe77fa51601d521baab5a9234f2e 最後嘗試ssh方式連線容器,發現可以無密碼登陸了~ [root@localhost ~]# ssh -p220 root@localhost Last login: Mon Mar 13 10:03:54 2017
---------------------------------------------------------------------------------------------------------------------
當登陸到容器後,可以檢視下容器ip
第一種方式: [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b220fabf815a wangssh "/usr/sbin/sshd -D" 6 hours ago Up About an hour 0.0.0.0:20020->22/tcp gigantic_goldwasser fc726a6a27d2 980e0e4c79ec "/bin/bash" 3 months ago Up About an hour 0.0.0.0:32768->80/tcp web1 9d99c7b9451b 980e0e4c79ec "/bin/bash" 3 months ago Up About an hour 0.0.0.0:32769->8080/tcp web3 [root@localhost ~]# docker inspect b220fabf815a |grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", 第二種方式: [root@localhost ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' b220fabf815a 172.17.0.2 第三種方式: 登陸到容器裡使用“yum install net-tools”,安裝後就可以使用ifconfig命令檢視ip了 當知道了容器的ip後,就可以使用ssh直接連線容器的22埠即可! [root@localhost ~]# ssh 172.17.0.2 root@172.17.0.2's password: Last login: Tue Mar 14 09:11:27 2017 from 172.17.0.1 [root@b220fabf815a ~]#