[Docker] 在 Mac 上安裝 Docker
最近 Docker 很紅,好奇的我也來嘗試看看~
主要是參考 Installing Docker on Mac OS X 這篇文章的說明~
2015/09/10 更新:boot2docker 已經 deprecated,
現在官方建議使用的是 Docker Machine,
可以參考 在 Mac 上改用 Docker Machine 取代 boot2docker 這篇。
1. 下載並安裝 Boot2Docker
Docker 是基於 Linux kernel 的技術達成的,因此要在 Mac 上執行 Docker,
必須在一個虛擬機器裡執行 Linux,在這 Linux 上執行 virtualized docker engine…
嗯…聽起來就很麻煩,但幸好已經有人幫忙做好苦工了 😛
Boot2Docker 這個專案提供了一個輕量的 Linux distribution,
讓我們方便執行 Docker。除此之外,還提供了一個安裝程式,
把這個 Linux distribution 和操作 VirtualBox 等的指令都包裝起來,簡化安裝的過程。
先從 https://github.com/boot2docker/osx-installer/releases/,
下載 Boot2Docker for Mac installer,我這邊下載的是 Book2Docker-1.4.1.pkg~
檔案有一百多 MB,下載較花時間… 下載好直接安裝:
安裝過程很迅速,可以瞄一下安裝完成畫面上的一些提醒,
這邊有提到可以執行 boot2docker upgrade 直接升級 Boot2Docker,不用重裝~
裝好之後,docker 和 boot2docker 的執行檔就已經放在 /usr/local/bin 目錄下了:
testuser@localhost ~ $ ll /usr/local/bin/*docker*
-rwxr-xr-x 1 root wheel 7511616 12 17 07:31 /usr/local/bin/boot2docker* -rwxr-xr-x 1 root wheel 7064960 12 17 04:12 /usr/local/bin/docker*
如果你這時候就照著畫面指示,執行 docker run hello-world 的話,就會遇到錯誤,
這是因為 hello-world 這個 container 並不存在:
testuser@localhost ~ $ docker run hello-world FATA[0000] Post http:///var/run/docker.sock/v1.16/containers/create: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?
2. 使用 Boot2Docker 建立新的 Linux VM
執行一下 boot2docker help,看一下它的說明:
testuser@localhost ~ $ boot2docker help Usage: boot2docker [<options>] <command> [<args>] Boot2Docker management utility. Commands: init Create a new Boot2Docker VM. up|start|boot Start VM from any states. ssh [ssh-command] Login to VM via SSH. save|suspend Suspend VM and save state to disk. down|stop|halt Gracefully shutdown the VM. restart Gracefully reboot the VM. poweroff Forcefully power off the VM (may corrupt disk image). reset Forcefully power cycle the VM (may corrupt disk image). delete|destroy Delete Boot2Docker VM and its disk image. config|cfg Show selected profile file settings. info Display detailed information of VM. ip Display the IP address of the VM's Host-only network. shellinit Display the shell commands to set up the Docker client. status Display current state of VM. download Download Boot2Docker ISO image. upgrade Upgrade the Boot2Docker ISO image (restart if running). version Display version information.
如果有碰過 VirtualBox 的 command line 工具 VBoxManage 的話,
就會注意到這些指令跟 VBoxManage 可以做的事很像,
就是可以建立新的 VM、開關機、建立/回復快照…等等~
事實上在 Boot2Docker 的網頁上也說明它的確是利用 VBoxManage 來作相關的的操作~
可以先用 VBoxManage 指令看一下現有的 VM:
testuser@localhost ~ $ VBoxManage list vms "WinXP" {77a583ce-05f5-42d8-af87-390a0cf677a0} "Win7_en" {93513461-ac5c-46ce-a217-c8dd0d1c5f72}
接著用 boot2docker init 這個指令建立一個新的 Linux VM,
它會去下載 boot2docker.iso,並建立 Linux VM:
testuser@localhost ~ $ 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/testuser/.boot2docker/boot2docker.iso Generating public/private rsa key pair. Your identification has been saved in /Users/testuser/.ssh/id_boot2docker. Your public key has been saved in /Users/testuser/.ssh/id_boot2docker.pub.
建立好之後,可以用 VBoxManage 指令查看這個新建好的 VM,
或是也可以直接開啟 VirtualBox app,可以看到有個新的 boot2docker-vm,
有興趣的可以看一下這個 VM 的一些設定,
基本的資訊是 Linux x64, 2GB RAM, 4 CPU (Boot2Docker 的 FAQ 也有寫):
VM 掛載了 boot2docker.iso 這個光碟,同時有個 20GB 的硬碟:
看到這裡應該會想說,這個硬碟 20GB 不知道夠不夠?
嗯… 我也不知道,之後如果有需要再來看看如何擴展吧 😛
3. 更新 Boot2Docker
在啟動這個 boot2docker-vm 之前,執行一下 boot2docker upgrade,看看有沒有更新…
它會去更新 docker 和 boot2docker,不過比較怪的是它好像每次都會去抓 boot2docker.iso,
即使你已經有最新版本的 iso 了… =_=
testuser@localhost ~ $ boot2docker upgrade docker is up to date (1.4.1), skipping upgrade... boot2docker is up to date (v1.4.1), skipping upgrade... 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/testuser/.boot2docker/boot2docker.iso
4. 啟動 boot2docker-vm 虛擬機器
執行 boot2docker start 來啟動這個 Linux 虛擬機器:
testuser@localhost ~ $ boot2docker start Waiting for VM and Docker daemon to start... .......................oooooooooooooo Started. Writing /Users/testuser/.boot2docker/certs/boot2docker-vm/ca.pem Writing /Users/testuser/.boot2docker/certs/boot2docker-vm/cert.pem Writing /Users/testuser/.boot2docker/certs/boot2docker-vm/key.pem To connect the Docker client to the Docker daemon, please set: export DOCKER_CERT_PATH=/Users/testuser/.boot2docker/certs/boot2docker-vm export DOCKER_TLS_VERIFY=1 export DOCKER_HOST=tcp://192.168.59.103:2376
執行 docker ssh 的話就可以用 SSH 連到這個 Linux VM 裡面,
可以看到這個 Linux 是用 TinyCore Linux 作的:
testuser@localhost ~ $ boot2docker ssh ## . ## ## ## == ## ## ## ## === /""""""""""""""""___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ ______ o __/ __/ __________/ _ _ ____ _ _ | |__ ___ ___ | |_|___ __| | ___ ___| | _____ _ __ | '_ / _ / _ | __| __) / _` |/ _ / __| |/ / _ '__| | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ | |_.__/ ___/ ___/ __|_______,_|___/ ___|_|____|_| Boot2Docker version 1.4.1, build master : 86f7ec8 - Tue Dec 16 23:11:29 UTC 2014 Docker version 1.4.1, build 5bc2ff8 docker@boot2docker:~$ uname -a Linux boot2docker 3.16.7-tinycore64 #1 SMP Tue Dec 16 23:03:39 UTC 2014 x86_64 GNU/Linux
其實步驟 2~4 的動作,也可以直接執行 Applications > boot2docker.app 來達到:
它會跳出一個 terminal 自動執行 boot2docker init, up, version 這幾個指令:
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 Last login: Fri Dec 19 08:40:23 on ttys002 You have mail. testuser@localhost ~ $ bash testuser@localhost ~ $ unset DYLD_LIBRARY_PATH ; unset LD_LIBRARY_PATH testuser@localhost ~ $ mkdir -p ~/.boot2docker testuser@localhost ~ $ if [ ! -f ~/.boot2docker/boot2docker.iso ]; then cp /usr/local/share/boot2docker/boot2docker.iso ~/.boot2docker/ ; fi testuser@localhost ~ $ /usr/local/bin/boot2docker init Virtual machine boot2docker-vm already exists testuser@localhost ~ $ /usr/local/bin/boot2docker up Waiting for VM and Docker daemon to start... .............oooo Started. Writing /Users/testuser/.boot2docker/certs/boot2docker-vm/ca.pem Writing /Users/testuser/.boot2docker/certs/boot2docker-vm/cert.pem Writing /Users/testuser/.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/testuser/.boot2docker/certs/boot2docker-vm export DOCKER_TLS_VERIFY=1 testuser@localhost ~ $ $(/usr/local/bin/boot2docker shellinit) Writing /Users/testuser/.boot2docker/certs/boot2docker-vm/ca.pem Writing /Users/testuser/.boot2docker/certs/boot2docker-vm/cert.pem Writing /Users/testuser/.boot2docker/certs/boot2docker-vm/key.pem testuser@localhost ~ $ 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 testuser@localhost ~ $
要注意 boot2docker up 指令執行後,裡面有一段說要設定一些環境變數,
記得要執行,不然接下來的 docker 指令都會錯誤:
(下面的是在我的電腦上的結果,請依自己電腦上的 boot2docker up 的輸出來作)
export DOCKER_HOST=tcp://192.168.59.103:2376 export DOCKER_CERT_PATH=/Users/testuser/.boot2docker/certs/boot2docker-vm export DOCKER_TLS_VERIFY=1
5. 使用 Docker 執行 Hello World 程式
執行 docker run hello-world 就可以執行 Docker 預放的一個測試程式,
成功的話,會像下面一樣去抓 hello-world 這個 image,然後秀出一段歡迎訊息~
testuser@localhost ~ $ docker run hello-world Unable to find image 'hello-world:latest' locally hello-world:latest: The image you are pulling has been verified 511136ea3c5a: Pull complete 7fa0dcdc88de: Pull complete ef872312fe1b: Pull complete Status: Downloaded newer image for hello-world:latest Hello from Docker. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (Assuming it was not already locally available.) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash For more examples and ideas, visit: http://docs.docker.com/userguide/
歡迎訊息裡有一個利用 Docker 在 Ubuntu 上執行 bash 的範例,
可以執行看看 docker run -it ubuntu bash:
testuser@localhost ~ $ docker run -it ubuntu bash Unable to find image 'ubuntu:latest' locally ubuntu:latest: The image you are pulling has been verified c7b7c6419568: Pull complete 70c8faa62a44: Pull complete d735006ad9c1: Pull complete 04c5d3b7b065: Pull complete 511136ea3c5a: Already exists Status: Downloaded newer image for ubuntu:latest root@5bd4bc87e24a:/# uname -a Linux 5bd4bc87e24a 3.16.7-tinycore64 #1 SMP Tue Dec 16 23:03:39 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux root@5bd4bc87e24a:/# df -h Filesystem Size Used Avail Use% Mounted on rootfs 19G 273M 17G 2% / none 19G 273M 17G 2% / tmpfs 1005M 0 1005M 0% /dev shm 64M 0 64M 0% /dev/shm /dev/sda1 19G 273M 17G 2% /etc/hosts tmpfs 1005M 0 1005M 0% /proc/kcore root@5bd4bc87e24a:/# exit exit
到目前為止,docker 算是成功安裝起來了~
但到底它可以拿來作什麼用呢?可能要再多嘗試看看再來分享了~^^
One thought on “[Docker] 在 Mac 上安裝 Docker”
寫的好清楚呀,讓新手快速上手:D
版主回覆:(07/08/2015 02:05:15 PM)
我也是 docker 新手,希望有幫上忙^^