# 啟動一個以 ubuntu image為主 的 container , 名稱命名為 jangmt0305 ,並將本地端的 tcp port 8080 對應到 container 80 ,並且 -i 將 STDIN 保留, -t 取得一個虛擬終端機,並執行 /bin/bash 的程序。
mtchang@mt ~ $ sudo docker run --name="jangmt0305" -p 8080:80 -i -t ubuntu /bin/bash
# 底下為在 docker container 內的 /bin/bash 執行
root@b29f95d30b68:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:09
inet addr:172.17.0.9 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:9/64 Scope:Link
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:508 (508.0 B) TX bytes:508 (508.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
# docker host 另開一個新視窗,從外部來看目前的 container 執行狀況
mtchang@mt ~ $ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b29f95d30b68 ubuntu:14.04 "/bin/bash" 36 seconds ago Up 35 seconds 0.0.0.0:8080->80/tcp jangmt0305
# 可以透過 Linux 的 iptables 看到系統開了一個 DNAT 對應 tcp dpt:8080 to:172.17.0.9:80
mtchang@mt ~ $ sudo iptables -L -n -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0
MASQUERADE tcp -- 172.17.0.9 172.17.0.9 tcp dpt:80
Chain DOCKER (2 references)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 to:172.17.0.9:80
# 回到 docker container 內,可以執行安裝程序。
root@b29f95d30b68:/# apt-get install apache2 php5-cli libapache2-mod-php5
# 啟動服務
root@b29f95d30b68:/# /etc/init.d/apache2 restart
* Restarting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.9. Set the 'ServerName' directive globally to suppress this message [ OK ]
root@b29f95d30b68:/# netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::80 :::* LISTEN -
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node PID/Program name Path
# 用瀏覽器測試看看觀看網址 http://:8080/
# 把剛剛的 container 重新載入啟動,需要記住 container ID 號碼,可以使用 docker ps -a 來查詢。
mtchang@mt ~ $ sudo docker start b29f95d30b68
b29f95d30b68
mtchang@mt ~ $ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b29f95d30b68 ubuntu:14.04 "/bin/bash" 19 minutes ago Up 6 seconds 0.0.0.0:8080->80/tcp jangmt0305
# 可以透過 docker attach 取回 container ,重新取得 /bin/bash
mtchang@mt ~ $ sudo docker attach b29f95d30b68
root@b29f95d30b68:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:0a
inet addr:172.17.0.10 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:a/64 Scope:Link
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:648 (648.0 B) TX bytes:648 (648.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
沒有留言:
張貼留言