Experience Docker


Docker 這東西我注意很久了,但一直以為他就一個 VM Guest Machine 的 Adapter 之類的東西,然後可以 remote 一些指令。不過我誤解了,因為如果只是這樣,應該紅不起來。看到這本書 Docker —— 從入門到實踐 裡面提到一段和 VM 的比較,如下圖節錄:

Screen Shot 2014-12-17 at 22.46.41.png

這段很吸引我,我想『感覺』一下啟動的『秒級』是啥感覺,還有效能接近『原生』。。。這太有吸引力了!!然後還是可以快速建立測試環境,也符合我現在想要的,甚至可以不用像 OpenStack 那麼複雜的安裝和設置,而且要很多 resource (就是機器要很強啦) 。。。

以下是我的感動筆記。。。。 XDD


Enviornment

Install

  • 直接安裝下載的 Boot2Docker-1.4.1.pkg, 跟一般在 Mac 上安裝軟體一樣
  • 到 LaunchPad 點選 boot2docker, 會開始初始化, 我的過程如下, 看起來應該沒問題:
    init boot2docker
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    rick@iWish:22:39:27 ~$ bash
    unset DYLD_LIBRARY_PATH ; unset LD_LIBRARY_PATH
    mkdir -p ~/.boot2docker
    if [ ! -f ~/.boot2docker/boot2docker.iso ]; then cp /usr/local/share/boot2docker/boot2docker.iso ~/.boot2docker/ ; fi
    /usr/local/bin/boot2docker init
    /usr/local/bin/boot2docker up
    $(/usr/local/bin/boot2docker shellinit)
    docker version
    rick@iWish:22:39:27 ~$ unset DYLD_LIBRARY_PATH ; unset LD_LIBRARY_PATH
    rick@iWish:22:39:27 ~$ mkdir -p ~/.boot2docker
    ocker.iso ~/.boot2docker/ ; fi-f ~/.boot2docker/boot2docker.iso ]; then cp /usr/local/share/boot2docker/boot2d
    rick@iWish:22:39:27 ~$ /usr/local/bin/boot2docker init
    Generating public/private rsa key pair.
    ... omitted ...
    rick@iWish:22:40:11 ~$ docker version
    Client version: 1.4.1
    Client API version: 1.16
    Go version (client): go1.3.3
    Git commit (client): 5bc2ff8
    OS/Arch (client): darwin/amd64
    Server version: 1.4.1
    Server API version: 1.16
    Go version (server): go1.3.3
    Git commit (server): 5bc2ff8
    rick@iWish:22:40:12 ~$

Install Image from Docker Hub - CentOS

我習慣使用 CentOS,就先來一個漱漱口,指令:docker pull centos。Docker 會到一個叫做 Docker Hub 的地方抓指定的 image,Docker Hub 上有很多各式各樣的 映像檔分類。以下是我抓的過程:

docker pull centos
1
2
3
4
5
6
rick@iWish:22:40:12 ~$ docker pull centos
centos:latest: The image you are pulling has been verified
511136ea3c5a: Pull complete
5b12ef8fd570: Pull complete
34943839435d: Pull complete
Status: Downloaded newer image for centos:latest

這個過程會抓到的是最新的 CentOS 7,如果想要指定版本,那就要下 docker pull centos:centos6

Run command in CentOS

docker run 是用來執行 container 裏的程式,後面可以帶參數,跑完後 container 就結束了,我執行幾個 linux command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
rick@iWish:23:19:44 ~$ docker run centos /bin/echo 'hello'
hello
rick@iWish:23:19:56 ~$ docker run centos /bin/cat /etc/issue
\S
Kernel \r on an \m

rick@iWish:23:22:41 ~$ docker run centos /bin/cat /etc/fstab
LABEL=_/ / ext4 defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
rick@iWish:23:22:47 ~$ docker run centos /bin/df
Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 19049892 310512 17748656 2% /
none 19049892 310512 17748656 2% /
tmpfs 1028332 0 1028332 0% /dev
shm 65536 0 65536 0% /dev/shm
/dev/sda1 19049892 310512 17748656 2% /etc/hosts
tmpfs 1028332 0 1028332 0% /proc/kcore

可以這樣想像,guest machine 不用真的開起來,然後就可以執行 container 裡面的程式。如果想開啟 shell:docker run -t -i centos /bin/bash-t 是虛擬終端機,-i 是開啟 stdin,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
rick@iWish:23:28:15 ~$ docker run -t -i centos
[root@983b17e4470a /]# pwd
/
[root@983b17e4470a /]# ls -ls
total 52
0 lrwxrwxrwx 1 root root 7 Dec 2 14:58 bin -> usr/bin
0 drwxr-xr-x 5 root root 380 Dec 17 15:28 dev
4 drwxr-xr-x 46 root root 4096 Dec 17 15:28 etc
4 drwxr-xr-x 2 root root 4096 Jun 10 2014 home
0 lrwxrwxrwx 1 root root 7 Dec 2 14:58 lib -> usr/lib
0 lrwxrwxrwx 1 root root 9 Dec 2 14:58 lib64 -> usr/lib64
4 drwx------ 2 root root 4096 Dec 2 14:58 lost+found
4 drwxr-xr-x 2 root root 4096 Jun 10 2014 media
4 drwxr-xr-x 2 root root 4096 Jun 10 2014 mnt
4 drwxr-xr-x 2 root root 4096 Jun 10 2014 opt
0 dr-xr-xr-x 93 root root 0 Dec 17 15:28 proc
4 dr-xr-x--- 2 root root 4096 Dec 2 15:00 root
4 drwxr-xr-x 7 root root 4096 Dec 2 15:00 run
0 lrwxrwxrwx 1 root root 8 Dec 2 14:58 sbin -> usr/sbin
4 drwxr-xr-x 3 root root 4096 Dec 2 15:00 selinux
4 drwxr-xr-x 2 root root 4096 Jun 10 2014 srv
0 dr-xr-xr-x 13 root root 0 Dec 17 15:28 sys
4 drwxrwxrwt 2 root root 4096 Dec 17 15:28 tmp
4 drwxr-xr-x 13 root root 4096 Dec 2 14:58 usr
4 drwxr-xr-x 18 root root 4096 Dec 2 14:59 var
[root@983b17e4470a /]# df -h
Filesystem Size Used Avail Use% Mounted on
rootfs 19G 305M 17G 2% /
none 19G 305M 17G 2% /
tmpfs 1005M 0 1005M 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/sda1 19G 305M 17G 2% /etc/hosts
tmpfs 1005M 0 1005M 0% /proc/kcore
[root@983b17e4470a /]# uname -a
Linux 983b17e4470a 3.16.7-tinycore64 #1 SMP Tue Dec 16 23:03:39 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@983b17e4470a /]# whoami
root
[root@983b17e4470a /]# netstat -na
bash: netstat: command not found
[root@983b17e4470a /]# ifconfig
bash: ifconfig: command not found
[root@983b17e4470a home]# exit
exit
rick@iWish:23:33:43 ~$

試了一些基本的東西,看看系統狀態,不過可以發現 netstatifconfig 都無法執行,應該需要額外設置 eth 之類的東西吧。

LAMP

我最常用的 LAMP 組合,找了適當的 image 裝: docker pull dockerfiles/centos-lamp
也可以先用 docker search [keyword] 找找看,下面是搜尋的結果 - 一堆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
rick@iWish:23:33:45 ~$ docker search lamp
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tutum/lamp LAMP image - Apache listens in port 80, an... 41 [OK]
dockerfiles/centos-lamp 6 [OK]
tehes/superdevlamp Complete optimized LAMP stack for Drupal d... 4
ricardoamaro/drupal-lamp https://github.com/ricardoamaro/docker-dru... 4
rpawel/lamp55-joomla 3
... 太多了,省略 ...
# 下載
rick@iWish:23:43:29 ~$ docker pull dockerfiles/centos-lamp
Pulling repository dockerfiles/centos-lamp
666d59f8429d: Download complete
539c0211cd76: Download complete
1b522efc5462: Download complete
46d50bbca148: Download complete
082a6a05108d: Download complete
f1c190401f1a: Download complete
987fe7539090: Download complete
7bfe4ff4df41: Download complete
720bdd4ad932: Download complete
27a235cf3931: Download complete
837dfe4536e0: Download complete
f977418995c6: Download complete
d638bd947021: Download complete
dc5d255a48c7: Download complete
b1089d528d27: Download complete
bc3211f4cc3e: Download complete
62d8c4d27eed: Download complete
Status: Downloaded newer image for dockerfiles/centos-lamp:latest

參數 -d 是啟動成 daemon。接續的 -p 49001:80 是指定和 local machine port mapping: 49001 對應到 80

start daemon
1
2
rick@iWish:23:56:40 ~$ docker run -d -p 49001:80 dockerfiles/centos-lamp
aef450a7b2092b0a5699e23bf48a01766ca2d6b9e08741f11864510aa326097c

檢查 Daemon 狀態:

docker ps
1
2
3
4
rick@iWish:23:57:34 ~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aef450a7b209 dockerfiles/centos-lamp:latest "supervisord -n" 21 seconds ago Up 21 seconds 22/tcp, 0.0.0.0:49001->80/tcp stoic_franklin
rick@iWish:23:59:29 ~$

打開瀏覽器,開啟 URL: http://localhost:49001 應該會看到 apache 的畫面 不過其實我沒跑成功, 待研究


結論

『感覺』過後,其實還不賴!真的『秒殺』的啟動時間。接下來我還想繼續是一些東西,像是:

  1. MySQL, MongoDB, Memcache
  2. Java Container: Tomcat / Jetty
  3. Multiple docker container
  4. Config network for docker container
  5. Docker life-cycle
  6. How to backup? portable?

如果跟我同樣再觀望,可以去官方的 Try It! 先試看看。


延伸閱讀

Reference



Comments

  • 全站索引
  • 關於這裏
  • 關於作者
  • 學習法則
  • 思考本質
  • 一些領悟
  • 分類哲學
  • ▲ TOP ▲