1. 程式人生 > >Docker實戰(三):Docker安裝ssh,supervisor等基礎工具

Docker實戰(三):Docker安裝ssh,supervisor等基礎工具

Docker安裝ssh,supervisor等基礎工具

需要提前下載好官方的ubuntu映象,我這裡使用的是ubuntu:14.04版本,這裡安裝了一些基礎的工具ssh,curl,wget,vim等等,包括後續的Docker映象需要啟動多個服務,所以提前先裝好supervisor。

Dockerfile檔案
############################################
# version : birdben/tools:v1
# desc : 當前版本安裝的ssh,wget,curl,supervisor 
########################
#################### # 設定繼承自ubuntu官方映象 FROM ubuntu:14.04 # 下面是一些建立者的基本資訊 MAINTAINER birdben (191654006@163.com) # 注意這裡要更改系統的時區設定,因為在 web 應用中經常會用到時區這個系統變數,預設的 ubuntu 會讓你的應用程式發生不可思議的效果哦 ENV DEBIAN_FRONTEND noninteractive # 清空ubuntu更新包 RUN sudo rm -rf /var/lib/apt/lists/* # 一次性安裝vim,wget,curl,ssh server等必備軟體
# RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list RUN sudo apt-get update RUN sudo apt-get install -y vim wget curl openssh-server sudo RUN sudo mkdir -p /var/run/sshd # 將sshd的UsePAM引數設定成no RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config # 新增測試使用者admin,密碼admin,並且將此使用者新增到sudoers裡
RUN useradd admin RUN echo "admin:admin" | chpasswd RUN echo "admin ALL=(ALL) ALL" >> /etc/sudoers # 把admin使用者的shell改成bash,否則SSH登入Ubuntu伺服器,命令列不顯示使用者名稱和目錄 RUN usermod -s /bin/bash admin # 安裝supervisor工具 RUN sudo apt-get install -y supervisor RUN sudo mkdir -p /var/log/supervisor # 新增 supervisord 的配置檔案,並複製配置檔案到對應目錄下面。(supervisord.conf檔案和Dockerfile檔案在同一路徑) COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf # 容器需要開放SSH 22埠 EXPOSE 22 # 執行supervisord來同時執行多個命令,使用 supervisord 的可執行路徑啟動服務。 CMD ["/usr/bin/supervisord"]
Dockerfile原始檔連結:
supervisor配置檔案內容
# 配置檔案包含目錄和程序
# 第一段 supervsord 配置軟體本身,使用 nodaemon 引數來執行。
# 第二段包含要控制的 2 個服務。每一段包含一個服務的目錄和啟動這個服務的命令。

[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D
supervisor原始檔連結:
控制檯終端
# 構建映象
$ docker build -t="birdben/tools:v1" .
# 執行已經構件好的映象,因為我使用的ubuntu虛擬機器安裝的Docker,而我的虛擬機器也安裝了ssh服務,所以這裡指定了宿主機的埠為9999對應Docker容器的22埠
$ docker run -p 9999:22 -t -i "birdben/tools:v1"


# 此時檢視宿主機的9999埠,已經處於監聽狀態:
$ netstat -aunpt
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -               
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -               
tcp6       0      0 :::9999                 :::*                    LISTEN      -               
tcp6       0      0 :::22                   :::*                    LISTEN      -   

# 再檢視一下宿主機的IP地址,我這裡的IP地址是10.211.55.4
$ ifconfig

# 此時可以通過ssh遠端連線Docker容器了
$ ssh root@10.211.55.4 -p 9999
# 輸入密碼應該就可以連線到Docker容器了

# 如果遇到下面的問題,這是Linux重灌或則openssh-server重灌引起的,執行以下命令即可
$ ssh-keygen -R 10.211.55.4

# 如果上述方式不好用,進入此目錄,刪除的10.211.55.4相關rsa的資訊即可
$ vi ~/.ssh/known_hosts


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
8c:4b:88:88:53:4a:b1:f0:e2:da:9a:dc:aa:67:46:df.
Please contact your system administrator.
Add correct host key in /Users/ben/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/ben/.ssh/known_hosts:18
RSA host key for [10.211.55.4]:9999 has changed and you have requested strict checking.
Host key verification failed.
遇到的問題和解決辦法
Q:ssh登入後,命令列不顯示使用者名稱和目錄
A:把使用者的shell改成bash,否則SSH登入Ubuntu伺服器,命令列不顯示使用者名稱和目錄
RUN usermod -s /bin/bash admin

參考:
http://bbs.csdn.net/topics/390188284

Q:ssh建立admin登入使用者,不使用root登入
A:這裡使用ssh不建議直接使用root使用者登入,建議建立一個新的使用者例如admin登入
RUN useradd admin
RUN echo "admin:admin" | chpasswd
RUN echo "admin   ALL=(ALL)       ALL" >> /etc/sudoers

參考:
http://blog.csdn.net/kongxx/article/details/38395305
http://blog.csdn.net/kongxx/article/details/38412119

Q:如何修改ssh服務相關配置
A:可以直接修改sshd_config配置檔案
vi /etc/ssh/sshd_config
需要修改如下

# 設定不允許root使用者登入
PermitRootLogin yes

# 利用 PAM 管理使用者認證有很多好處,可以記錄與管理。
# 所以這裡我們建議你使用 UsePAM 且 ChallengeResponseAuthentication 設定為 no,但是我們這裡為了簡單設定為密碼認證,ChallengeResponseAuthentication設定為yes,UsePAM設定為no
ChallengeResponseAuthentication yes
UsePAM no

參考:
http://my.oschina.net/fsmwhx/blog/143354
http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646032.html

參考文章: