1. 程式人生 > >【Docker江湖】之建立帶有SSH服務的映象

【Docker江湖】之建立帶有SSH服務的映象

這裡寫圖片描述
開啟微信掃一掃,關注微信公眾號【資料與演算法聯盟】

轉載請註明出處:http://blog.csdn.net/gamer_gyt
博主微博:http://weibo.com/234654758
Github:https://github.com/thinkgamer

Docker江湖

寫在前邊的話

   一般情況下,linux作業系統的管理員通過SSH服務來管理作業系統,但是Docker的很多映象都是不帶SSH服務的,接下來我們就來看一下如何建立一個帶有SSH服務的映象

基於Commit命令建立

1:準備一個ubuntu的映象

sudo docker pull ubuntu

預設安裝最新版,檢視映象

sudo docker images

這個時候便可以看到我們pull的ubuntu映象了

這裡寫圖片描述

2:啟動映象,進入容器

[[email protected] ~]$ sudo docker run -it -d ubuntu:latest /bin/bash
[sudo] password for redhat:
e968f75ffc88881377ac0b5b74bd273c3c516544ff7d6270a1683aa676da3d6c
[[email protected] ~]$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e968f75ffc88        ubuntu:latest       "/bin/bash"
18 seconds ago Up 16 seconds cranky_stallman [[email protected] ~]$ sudo docker exec -it e9 /bin/bash

嘗試使用sshd命令,會發現容器中並沒有安裝此命令

root@e968f75ffc88:/# sshd
bash: sshd: command not found

嘗試安裝openssh-server

[email protected]:/# apt-get install openssh-server
Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package openssh-server

更新軟體源

apt-get update

3:安裝和配置ssh服務

apt-get install openssh-server

要正常啟動ssh服務,需要目錄/var/run/sshd存在,手動建立他,並啟動服務

mkdir -p /var/run/sshd
/usr/sbin/sshd -D &

此時檢視容器的22埠(SSH服務預設監聽的埠),已經處於監聽狀態

apt-get install net-tools
netstat -tunlp

root@e968f75ffc88:/# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3551/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      3551/sshd

修改SSH服務的安全登入配置,取消pam登陸限制

sed -ri ‘s/session required pam_loginuid.so/#session required pam_loginuid.so/g’ /etc/pam.d/sshd

root使用者目錄下建立.ssh目錄,並複製需要登入的公鑰資訊(一般為本地主機使用者目錄下的.ssh/id_rsd.pub檔案,可由ssh-keygen -t rsa命令生成)到authorized_keys檔案中

mkdir root/.ssh
apt-get install vim
vim /root/.ssh/authorized_keys

建立自動啟動SSH服務的可執行檔案run.sh,並新增可執行許可權:

vim /run.sh
chmod +x run.sh

run.sh的內容如下:

#!/bin/bash
/usr/sbin/sshd -D

最後退出容器:

exit

4:儲存映象

sudo docker commit fcl sshd:ubuntu

檢視本地映象,就會看到新生成的映象

```
[redhat@localhost ~]$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
e968f75ffc88        ubuntu:latest       "/bin/bash"         About an hour ago   Exited (0) About a minute ago                       cranky_stallman
[redhat@localhost ~]$ sudo docker commit e96 sshd:ubuntu
sha256:f52e07fa7accf437f52cb39cd36cdab9229ef88b2280129ff4d2c272fbb73aad
[redhat@localhost ~]$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
sshd                ubuntu              f52e07fa7acc        About a minute ago   255 MB
ubuntu              latest              f753707788c5        2 weeks ago          127.1 MB

使用映象,並新增埠對映(10022–>22),其中10022是宿主主機的埠,22是容器SSH服務監聽埠

sudo docker run -it -d -p 10022:22 sshd:ubuntu_new /run.sh

SSH測試登入

ssh you_ip -p 10022

[[email protected] .ssh]# ssh 192.168.10.179 -p 10022
The authenticity of host '[192.168.10.179]:10022 ([192.168.10.179]:10022)' can't be established.
ECDSA key fingerprint is 0b:ae:62:09:a2:18:4e:ef:16:e3:3f:b9:2d:15:fb:7a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.10.179]:10022' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 3.10.0-229.el7.x86_64 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

[email protected]:~# exit
logout
Connection to 192.168.10.179 closed.

使用Dockerfile建立

1:建立工作目錄

首先建立一個sshd_ubuntu目錄

mkdir ssh_ubuntu

在其中,建立Dockerfile和run.sh檔案

[root@localhost mydockerfile]# cd ssh_ubuntu/
[root@localhost ssh_ubuntu]# touch Dockerfile run.sh
[root@localhost ssh_ubuntu]# ls
Dockerfile  run.sh
[root@localhost ssh_ubuntu]#

指令碼檔案run.sh的內容與上面的內容一致:

#!/bin/bash
/usr/sbin/sshd -D

在宿主主機上生成SSh金鑰對,並建立authorized_keys檔案:

cat ~/.ssh/id_rsa.pub > authorized_keys

3:編寫Dockerfile

下面是Dockerfile內容及各部分註釋,和上邊commit映象的步驟操作是一致的

#設定繼承映象
FROM ubuntu:latest

#執行命令
RUN apt-get update

#安裝ssh
RUN apt-get install -y openssh-server
RUN mkdir -p /var/run/sshd
RUN mkdir -p /root/.ssh

#取消pam限制
RUN sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd

#複製配置檔案到相應位置,並賦予指令碼可執行許可權
ADD authorized_keys /root/.ssh/authorized_keys
ADD run.sh /run.sh
RUN chmod 755 /run.sh

#開放埠
EXPOSE 22

#設定自啟動命令
CMD ["/run.sh"]

4:建立映象

在sshd_ubuntu 目錄下,使用docker build 命令來建立映象,注意一下,在最後還有一個“.” ,表示使用當前目錄中的Dockerfile

cd sshd_ubuntu
sudo docker build -t sshd:dockerfile .

這裡有一點需要注意的是使用Dockerfile建立自定義映象,docker會自動刪除中間臨時建立的層,還需要注意每一步的操作和編寫的dockerfile中命令的對應關係
執行docker build命令的輸出參考結果如下:

命令執行完畢後,如果可見 “successfully build XXX”字樣,則說明映象建立成功,可以看到,以上命令生成的映象ID是

Step 11 : CMD /run.sh
 ---> Running in 69e4227186fb
 ---> f530a7a43fd7
Removing intermediate container 69e4227186fb
Successfully built f530a7a43fd7

在本地檢視sshd:dokcerfile映象已存在:

[root@localhost ssh_ubuntu]# sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
sshd                dockerfile          f530a7a43fd7        About a minute ago   220.6 MB
sshd                ubuntu              cc0c1d242d82        38 minutes ago       255 MB

5:測試映象,執行容器

使用剛才建立的sshd:dockerfile映象來執行一個容器,直接啟動映象,對映容器的22埠到本地10122埠:

sudo docker run -d -p 10122:22 sshd:dockerfile
ssh 192.168.10.179 -p 10122

顯示:

[[email protected] .ssh]# ssh 192.168.10.179 -p 10122
The authenticity of host '[192.168.10.179]:10122 ([192.168.10.179]:10022)' can't be established.
ECDSA key fingerprint is 0b:ae:62:09:a2:18:4e:ef:16:e3:3f:b9:2d:15:fb:7a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.10.179]:10022' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 3.10.0-229.el7.x86_64 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

[email protected]:~# exit
logout
Connection to 192.168.10.179 closed.

Over!