2015年1月27日

Mac OS X 裡安裝 Docker

最近 Docker 成了當紅炸子雞,公司內部也將開發的測試環境,開始逐漸移轉到 Docker 上,在此介紹一下 Mac OS X 環境安裝 Docker 的程序

Step 1.
首先安裝 Vitual box https://www.virtualbox.org/wiki/Downloads
在下載的網頁裡面可以選擇下載 OS X hosts ,目前是 4.3.20 版
下載後請直接安裝。

Step 2.
用Homebrew 來安裝 docker

$ brew install docker

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/docker-1.4.1.yosemite.bottle.tar.gz
Already downloaded: /Library/Caches/Homebrew/docker-1.4.1.yosemite.bottle.tar.gz
==> Pouring docker-1.4.1.yosemite.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completion has been installed to:
  /usr/local/share/zsh/site-functions


Step 3.
安裝這次的重點 boot2docker,boot2docker 會用 Virtualbox 建立 Linux VM,在 Linux VM 上開啟 docker daemon 後,就可以直接對 VM 裡的 docker 下控制命令。
我們來繼續安裝 boot2docker

$ brew install boot2docker

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/boot2docker-1.4.1.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring boot2docker-1.4.1.yosemite.bottle.tar.gz
==> Caveats
To have launchd start boot2docker at login:
    ln -sfv /usr/local/opt/boot2docker/*.plist ~/Library/LaunchAgents
Then to load boot2docker now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.boot2docker.plist

Step 4.
使用 boot2docker init 指令,建立起 VM 的環境
    
$ boot2docker init

Latest release for boot2docker/boot2docker is v1.4.1
Downloading boot2docker ISO image...
Success: downloaded https://github.com/boot2docker/boot2docker/releases/download/v1.4.1/boot2docker.iso
    to /Users/martin/.boot2docker/boot2docker.iso
Generating public/private rsa key pair.
Your identification has been saved in /Users/martin/.ssh/id_boot2docker.
Your public key has been saved in /Users/martin/.ssh/id_boot2docker.pub.
The key fingerprint is:
4e:1d:89:54:78:95:e7:f2:10:8c:11:90:a0:02:f1:a6 martin@Martin-MacBook-Pro-2.local
The key's randomart image is:
+--[ RSA 2048]----+
|o.    ..o=+*..   |
| o   . .o.o.+ .  |
|  + .   ..o  +   |
| o .     . .o .  |
|E       S .  +   |
|       o      .  |
|        .        |
|                 |
|                 |
+-----------------+

Step 5.
啟動 boot2docker

$ boot2docker start

Waiting for VM and Docker daemon to start...
..........................................oooooooooooooooooooooooooooooooooo
Started.
Writing /Users/martin/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/martin/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/martin/.boot2docker/certs/boot2docker-vm/key.pem

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://192.168.59.103:2376
    export DOCKER_CERT_PATH=/Users/martin/.boot2docker/certs/boot2docker-vm
    export DOCKER_TLS_VERIFY=1

Step 6.
記得一一使用 export 來將這些環境變數設定在Mac OS X 裡
PS: 這裡的環境變數請參考 Step 5 啟動 boot2docker 後的顯示說明

$export DOCKER_HOST=tcp://192.168.59.103:2376
$export DOCKER_CERT_PATH=/Users/martin/.boot2docker/certs/boot2docker-vm
$export DOCKER_TLS_VERIFY=1

Step 7.
使用 docker version 查看一下是否已經安裝成功並且成功啟用,如果有出現以下訊息,那就確認安裝沒問題。
    
$ docker version

Client version: 1.4.1
Client API version: 1.16
Go version (client): go1.4
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

您現在已經可以開始使用 docker 指令,下載 Image ,操作docker container 。