標籤:mac os x 虛擬機器 docker linux核心 虛擬化
一、簡介
Docker是一個開源的容器引擎,能提供虛機化,從而達到隔離應用,可移植到任意安裝有Docker的機器上運行,這對開發人員來說是一個極大的福音。
由於工作上原因,最近準備開始使用Docker(公司裝Docker的時候,linux server核心版本太低,需要重新編譯)。
本文主要講解如何在Mac OSX上安裝Docker。
Tips:由於Docker引擎是使用了特定於Linux核心的特性,所以需要安裝一個輕量級的虛擬機器(如VirtualBox)來在OSX上運行。
所以需要下載官方提供的Boot2Docker來運行Docker守護進程。
二、Docker安裝1. 安裝 boot2docker/osx-installer找到最新的Release版本,https://github.com/boot2docker/osx-installer/releases, 下載PKG,直接安裝.
雙擊下載後的PKG包,進入安裝嚮導:
安裝完畢後,會發現你的Application裡面多了2個程式:
這就是剛才我們提到的Boot2Docker和VM:
至此,安裝步驟完畢。
三、配置啟動Docker在安裝完畢後boot2docker後,就可以直接使用boot2docerk這個client command來操作vm中Images了。1、boot2docker init 命令進行初始化
從日誌可以看出,初始化的過程是下載一個boot2docer.iso,然後會用ssh產生用於docker的ssh的公開金鑰和私密金鑰對,用於遠程。
shengli-mac$ boot2docker initLatest release for boot2docker/boot2docker is v1.3.2Downloading boot2docker ISO image...Success: downloaded https://github.com/boot2docker/boot2docker/releases/download/v1.3.2/boot2docker.isoto /Users/shengli/.boot2docker/boot2docker.isoGenerating public/private rsa key pair.Your identification has been saved in /Users/shengli/.ssh/id_boot2docker.Your public key has been saved in /Users/shengli/.ssh/id_boot2docker.pub.The key fingerprint is:ff:7a:53:95:e6:44:27:70:e1:ac:0a:b5:02:35:72:29 [email protected]The key‘s randomart image is:+--[ RSA 2048]----+| . +. ..o. || E+.. +...|| .. . +.o|| . . . . +.|| S . . +. || + . .. || o . || .o || .o.. |+-----------------+
2、啟動
boot2docerk start命令來啟動
shengli-mac$ boot2docker startWaiting for VM and Docker daemon to start..........................oooooooooooooooooooStarted.Writing /Users/shengli/.boot2docker/certs/boot2docker-vm/ca.pemWriting /Users/shengli/.boot2docker/certs/boot2docker-vm/cert.pemWriting /Users/shengli/.boot2docker/certs/boot2docker-vm/key.pemTo connect the Docker client to the Docker daemon, please set: export DOCKER_HOST=tcp://192.168.59.103:2376 export DOCKER_CERT_PATH=/Users/shengli/.boot2docker/certs/boot2docker-vm export DOCKER_TLS_VERIFY=1
Ps:這裡HOST為192.168.59.103,這個就是以後要訪問的虛擬機器的地址。
shengli-mac$ $(/usr/local/bin/boot2docker shellinit)Writing /Users/shengli/.boot2docker/certs/boot2docker-vm/ca.pemWriting /Users/shengli/.boot2docker/certs/boot2docker-vm/cert.pemWriting /Users/shengli/.boot2docker/certs/boot2docker-vm/key.pemshengli-mac$ docker versionClient version: 1.3.2Client API version: 1.15Go version (client): go1.3.3Git commit (client): 39fa2faOS/Arch (client): darwin/amd64Server version: 1.3.2Server API version: 1.15Go version (server): go1.3.3Git commit (server): 39fa2fa
至此,配置完畢。
如果想進入到虛擬機器的控制台,可以使用boot2docker ssh命令
shengli-mac$ boot2docker ssh ## . ## ## ## == ## ## ## ## === /""""""""""""""""\___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\______/ _ _ ____ _ _| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __| ‘_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ ‘__|| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ ||_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|Boot2Docker version 1.3.2, build master : 495c19a - Mon Nov 24 20:40:58 UTC 2014Docker version 1.3.2, build 39fa2fa
四、第一個和第二個Docker程式對於第一個,相信大家都很瞭解,Docker版的HelloWorld。這個正好來驗證是否安裝成功。1. Docker HelloWorld運行 docker run hello-world,docker會在本地尋找鏡像是否存在(肯定不存在了,剛裝的= =), 然後docker會去遠程Docker registry server去下載這個鏡像。下載後運行這個鏡像。這個鏡像程式,會輸出一句 Hello from Docker. 如果看到這句話,說明你的Docker已經正確安裝了。
shengli-mac$ docker run hello-worldUnable to find image ‘hello-world‘ locallyhello-world:latest: The image you are pulling has been verified511136ea3c5a: Pull complete 7fa0dcdc88de: Pull complete ef872312fe1b: Pull complete Status: Downloaded newer image for hello-world:latestHello 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 bashFor more examples and ideas, visit: http://docs.docker.com/userguide/
2、Docker Nginx這裡會示範下載一個Nginx Server的鏡像,然後可以通過本機訪問Docker的容器連接埠,從而訪問到部署在Docker中的Nginx。
docker run --rm -i -t -p 80:80 nginx
shengli-mac$ docker run --rm -i -t -p 80:80 nginxUnable to find image ‘nginx‘ locallynginx:latest: The image you are pulling has been verifiedf10807909bc5: Pull complete f6fab3b798be: Pull complete d21beea329f5: Pull complete 04499cf33a0e: Pull complete 34806d38e48d: Pull complete 343c8475e349: Pull complete 92360dceda1a: Pull complete 9495a555677d: Pull complete 911e2df03930: Pull complete 88daddf047c6: Pull complete db8a64f62509: Pull complete e46b3488b010: Pull complete 511136ea3c5a: Already exists Status: Downloaded newer image for nginx:latest
運行後,該命令會listen監聽訪問,如果成功訪問,則會看到nginx的log:
192.168.59.3 - - [07/Dec/2014:02:24:06 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36" "-"2014/12/07 02:24:06 [error] 7#0: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.59.3, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.59.103"192.168.59.3 - - [07/Dec/2014:02:24:06 +0000] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36" "-"
將nginx鏡像運行在Docker的80連接埠,那麼你訪問DOCKER_HOST:80將會看到nginx的歡迎頁面。
如果你忘記了dock的host,可以使用:
boot2docker ip
shengli-mac$ boot2docker ipThe VM‘s Host only interface IP address is: 192.168.59.103
五、總結
本文講解了,如何在Mac OSX安裝和部署Docker,沒有特別難的步驟,VM啟動也是後台進程,很輕量級。
/Applications/VirtualBox.app/Contents/MacOS/VBoxHeadless --comment boot2docker-vm --startvm 89b84e20-fd75-4ae6-a437-c3b2778aeed1 --vrde config
——EOF——
參考:https://docs.docker.com/installation/mac/
原創文章,轉賬請註明出自:http://blog.csdn.net/oopsoom/article/details/41785999
Docker 安裝 on Mac OS X