安装Docker之OSX

安装Docker——OSX

由于Docker使用了Linux特有的特性LXC,因此无法直接在OS X上运行。必须通过Boot2Docker 来安装,这个程序包括了:

  • VirtualBox Virtual Machine (VM)
  • Docker本身
  • Boot2Docker管理工具

安装Boot2Docker

https://github.com/boot2docker/osx-installer/releases/latest 获取最新的Boot2Docker安装,或者:

1
$ brew install boot2docker

创建VM:

1
2
3
4
5
6
7
8
$ boot2docker init
Latest release for boot2docker/boot2docker is v1.8.3
Downloading boot2docker ISO image...
Success: downloaded https://github.com/boot2docker/boot2docker/releases/download/v1.8.3/boot2docker.iso
to /Users/LeslieZhu/.boot2docker/boot2docker.iso
Generating public/private rsa key pair.
Your identification has been saved in /Users/LeslieZhu/.ssh/id_boot2docker.
Your public key has been saved in /Users/LeslieZhu/.ssh/id_boot2docker.pub.

启动VM:

1
2
3
4
5
6
7
8
9
10
11
12
$ boot2docker start
Waiting for VM and Docker daemon to start...
.........................ooooooooooooooo
Started.
Writing /Users/LeslieZhu/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/LeslieZhu/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/LeslieZhu/.boot2docker/certs/boot2docker-vm/key.pem

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

在Docker客户端显示VM环境信息:

1
2
3
4
5
6
7
$ boot2docker shellinit
Writing /Users/LeslieZhu/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/LeslieZhu/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/LeslieZhu/.boot2docker/certs/boot2docker-vm/key.pem
export DOCKER_HOST=tcp://192.168.59.103:2376
export DOCKER_CERT_PATH=/Users/LeslieZhu/.boot2docker/certs/boot2docker-vm
export DOCKER_TLS_VERIFY=1

设置VM环境:

1
$ eval "$(boot2docker shellinit)"

确认安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
Pulling repository docker.io/library/hello-world
af340544ed62: Download complete
535020c3e8ad: Download complete
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world: this image was pulled from a legacy registry. Important: This registry version will not be supported in future versions of docker.

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.
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.

Boot2Docker基本使用

查看boot2docker、docker状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ boot2docker status
running

$ 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.8.3
Server API version: 1.20
Go version (server): go1.4.2
Git commit (server): f4bf5c7

访问容器端口

启动一个nginx:

1
2
3
4
5
6
7
$ docker run -d -P --name web nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
Pulling repository docker.io/library/nginx
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx: this image was pulled from a legacy registry. Important: This registry version will not be supported in future versions of docker.
03c469238a81db48c85811260df2be8b624667f8455d69a79459e4ba69c51291

查看运行的docker容器:

1
2
3
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03c469238a81 nginx "nginx -g 'daemon of 40 seconds ago Up 39 seconds 0.0.0.0:32768->443/tcp, 0.0.0.0:32769->80/tcp web

查看容器端口:

1
2
3
$ docker port web
443/tcp -> 0.0.0.0:49156
80/tcp -> 0.0.0.0:49157

这里可以看到将容器的80端口映射到了本地机器的49157端口,可以访问: http://localhost:49157

获取在boot2docker VM的真实ip:

1
2
$ boot2docker ip
192.168.59.103

访问: http://192.168.59.103:49157

停止容器的web服务:

1
2
$ docker stop web
$ docker rm web

为容器挂载磁盘

当运行boot2docker的时候,默认挂载/Users目录到容器中,可以基于此目录,为容器挂载其他目录。

切换到家目录:

1
2
3
4
$ cd $HOME
$ mkdir site
$ cd site
$ echo "my new site" > index.html

挂载新目录:

1
2
$ docker run -d -P -v $HOME/site:/usr/share/nginx/html --name mysite nginx
6f488a5ca8e887f25fbc92fcb71f8b2e7b7874ecf4ccf4754553dd83c0b0e573

获取mysite容器的ip:

1
2
3
$ docker port mysite
443/tcp -> 0.0.0.0:32770
80/tcp -> 0.0.0.0:32771

此时可以访问到网页内容。

停止:

1
2
$ docker stop mysite
$ docker rm mysite