2015年5月5日 星期二

Docker 映像系統 (dafu/mybase) 應用(一) - 固定 IP 與 OpenSSH 伺服器

dafu/mybase 映像系統, 已安裝好 OpenSSH 伺服器, 所以根據此映像系統建立的軟體貨櫃, 啟動時會一併啟動 OpenSSH 伺服器, 除了具有 OpenSSH 伺服器, 還可設定固定 IP, 請看以下操作說明 :

1. 建立 myip 軟體貨櫃
以下 docker run 命令中, "
--cap-add=NET_ADMIN" 這個參數主要是允許 myip 軟體貨櫃, 可以執行網路相關設定命令, 例如 : ifconfig eth0, route add,....
$ sudo docker run --cap-add=NET_ADMIN --name=myip -i -t dafu/mybase /bin/bash
706766fe1019: Pull complete
a62a42e77c9c: Pull complete
2c014f14d3d9: Pull complete
b7cf8f0d9e82: Pull complete
9ac03a835e1a: Pull complete
d43d2365fe0e: Already exists
Digest: sha256:345c00320b0be6f925b59da9781180b90852be1f53a80f618e93125d96cac272
Status: Downloaded newer image for dafu/mybase:latest
 * Starting OpenBSD Secure Shell server sshd                                 [ OK ]
2. 撰寫 /etc/mystart.sh 程序檔
dafu/mybase 映像系統的設計, 你可以撰寫 /etc/mystart.sh 程序檔, 來設定 myip 軟體貨櫃的網路系統, 例如 : IP 位址, Default Gateway,....
root@11481ac78ea5:/# nano /etc/mystart.sh
#!/bin/bash

ifconfig eth0 down
sleep 2
ifconfig eth0 172.17.0.22 netmask 255.255.0.0 up
route add default gw 172.17.42.1
echo "nameserver 168.95.1.1" > /etc/resolv.conf
3. 重新啟動 myip 軟體貨櫃 root@11481ac78ea5:/# exit
exit
$ sudo docker start myip
myip
$ sudo docker attach myip

4. 檢視 myip 軟體貨櫃的 IP 位址
root@11481ac78ea5:/# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:04
          inet addr:172.17.0.22  Bcast:172.17.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:4/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:12 errors:0 dropped:2 overruns:0 frame:0
          TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1017 (1.0 KB)  TX bytes:1373 (1.3 KB)
5. 離開 myip 軟體貨櫃 按 Ctrl + p, Ctrl + q 鍵, 離開 myip 軟體貨櫃 6. 使用 ssh 命令, 登入 myip 軟體貨櫃 在 dafu/mybase 映像系統, 已建立 bigred 帳號, 密碼為 bigred $ ssh bigred@172.17.0.22
Warning: Permanently added '172.17.0.22' (ECDSA) to the list of known hosts.
bigred@172.17.0.22's password:
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-34-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Fri Apr 24 06:59:54 2015 from 192.168.77.11
輸入 whoami 命令, 執行結果如下 : bigred@11481ac78ea5:~$ whoami
bigred
7. 中斷 ssh 連線 bigred@11481ac78ea5:~$ exit
logout
Connection to 172.17.0.22 closed.