How to Use Docker on OS X: The Missing Guide (杂译)

来源:互联网 发布:视频剪辑合成配音软件 编辑:程序博客网 时间:2024/05/20 20:01

2015-02-01 wcdj


摘要:看到朋友推荐的一篇Docker在Mac OS X使用的E文,在学习的过程中顺便翻译部分关键内容,而非咬文嚼字的匹配翻译。


原文地址:
How to Use Docker on OS X: The Missing Guide
author: Chris Jones

译者:delphiwcdj (gerryyang)



首先,作者开篇提到Docker目前已成为大家茶前饭后讨论最多的一个话题,因为Docker出现使得运行和管理containers变得极其容易。伴随Docker技术的兴起,必将进一步促进DevOps技术的发展。

作者推荐《The Docker Book》这样一部好书(non-free),但同时指出,此书主要针对的是Linux用户,而非Mac OS X用户,因此针对OS X环境的一些问题,书中并没有给出详细的说明和解释。所以,作者希望通过本篇文章,重点针对OS X用户,给出一些补充和建议,下面分别通过几个部分来说明。

1 Docker是如何工作的

Docker是一个client-server应用程序,其中,Docker server以daemon的方式运行,并通过REST API为外部提供各种服务;而Docker client是一个基于命令行的客户端,主要负责向Docker server传递要执行的命令。
Docker运行所在的主机被称为Docker host,此主机只要是一个可以运行Linux的硬件设备即可,并没有太多其他要求。


2 Docker on Linux

Docker在Linux上部署会是什么样子?原来如此简单。


3 Docker on OS X

言归正传,本文的重点是讨论OS X而不是Linux。二者有哪些区别呢?由于内核的不同,OS X并不是运行Docker的原生环境,因此必须先在OS X上创建一个虚拟的Linux运行环境。boot2docker(a lightweight Linux distribution based onTiny Core Linux made specifically to run Docker containers. It runs completely from RAM, weighs ~27MB and boots in ~5s (YMMV))针对这个问题给出了完美的解决方案。是的!Docker要在Mac的boot2docker VM上才能运行。


好吧,开始我们的实践之旅。


4 安装

Step 1: 安装 VirtualBox

Step 2: 安装 Docker 和 boot2docker
有两种方式可供选择。通过the Docker site官方的package安装,或者使用homebrew进行安装。作者建议使用homebrew命令行的方式安装。

> brew update> brew install docker> brew install boot2docker

Step 3: 初始化和启动 boot2docker
第一运行boot2docker时需要使用boot2docker init进行一次初始化,然后就可以boot2docker up启动VM了。

> boot2docker init2014/08/21 13:49:33 Downloading boot2docker ISO image...    [ ... ]2014/08/21 13:49:50 Done. Type `boot2docker up` to start the VM.> boot2docker up2014/08/21 13:51:29 Waiting for VM to be started..........2014/08/21 13:51:50 Started.2014/08/21 13:51:51   Trying to get IP one more time2014/08/21 13:51:51 To connect the Docker client to the Docker daemon, please set:2014/08/21 13:51:51     export DOCKER_HOST=tcp://192.168.59.103:2375

Step 4: 设置 DOCKER_HOST 环境变量
在.bashrc设置DOCKER_HOST环境变量,即告知Docker client的Docker host指向boot2docker VM。实际的地址请根据boot2docker up提示的信息进行设置。

> export DOCKER_HOST=tcp://192.168.59.103:2375

Step 5: 测试效果

> docker infoContainers: 0Images: 0Storage Driver: aufs Root Dir: /mnt/sda1/var/lib/docker/aufs Dirs: 0Execution Driver: native-0.2Kernel Version: 3.15.3-tinycore64Debug mode (server): trueDebug mode (client): falseFds: 10Goroutines: 10EventsListeners: 0Init Path: /usr/local/bin/dockerSockets: [unix:///var/run/docker.sock tcp://0.0.0.0:2375]
让我们总结下:we’ve set up a VirtualBox VM running boot2docker. The VM runs the Docker server, and we’re communicating with it using the Docker client on OS X.


Bueno!让我们的容器跑起来吧。


5 常见问题

Problem #1:Port Forwarding
问题描述
:boot2docker负责Docker容器和宿主主机的端口映射,而不是OS X。
我们使用nginx镜像启动一个容器:

> docker run -d -P --name web nginxUnable to find image 'nginx' locallyPulling repository nginx    [ ... ]0092c03e1eba5da5ccf9f858cf825af307aa24431978e75c1df431e22e03b4c3

This command starts a new container as a daemon (-d), automatically forwards the ports specified in the image (-P), gives it the name ‘web’ (--name web), and uses thenginx image. Our new container has the unique identifier 0092c03e1eba....

验证容器是否正常运行:

> docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES0092c03e1eba        nginx:latest        nginx               44 seconds ago      Up 41 seconds       0.0.0.0:49153->80/tcp   web

Under the PORTS heading, we can see our container exposes port 80, and Docker has forwarded this port from the container to a random port, 49153, on the host.

使用curl测试我们的站点:

> curl localhost:49153curl: (7) Failed connect to localhost:49153; Connection refused
为什么访问失败了呢?没错,虽然Docker容器的80端口被映射到宿主主机的49153端口,但若是Linux,宿主主机是localhost,然而我们是在VM中,宿主主机并非是localhost。
解决方法:使用VM的IP地址访问。其中,可以使用 boot2docker ip 命令来获取VM的IP地址。

> boot2docker ipThe VM’s Host only interface IP address is: 192.168.59.103<pre class="no-highlight">> curl $(boot2docker ip):49153The VM’s Host only interface IP address is:<!DOCTYPE html><html><head><title>Welcome to nginx!</title>    [ ... ]

为了更方便,可以添加host来访问:

> echo $(boot2docker ip) dockerhost | sudo tee -a /etc/hosts

Problem #2: Mounting Volumes
问题描述
:Docker从boot2docker VM挂载数据卷,而不是OS X。
你可以将宿主主机的一个目录挂载到容器中,数据卷提供给容器一种可以访问外部数据的能力。


Problem #3: Getting Inside a Container
问题描述
:怎么样才能进入到容器里呢?


6 结束

想学习更多关于Docker的细节,可以参考《The Docker Book》(Free,已上传)。



0 0
原创粉丝点击