imsobear / blog

果同学的博客
161 stars 9 forks source link

Docker-1: 在 Mac OSX 上安装 boot2docker #55

Closed imsobear closed 7 years ago

imsobear commented 9 years ago

因为 docker 引擎使用的是 Linux 内核的特性,所以想要在 OSX上运行 docker,我们就需要在 MAC 上提供一个虚拟机环境,那这个环境就是 boot2docker 这个工具。

1. 安装 VirtualBox:

直接去 VirtualBox Downloads 下载对应的 .dmg 文件然后安装即可。

这时候打开 VirtualBox 里面是没有虚拟机的。

2. 安装 boot2docker: brew install boot2docker

3. boot2docker初始化:

在正常的国度,只需要直接运行 boot2docker init 即可,不过,因为某些大家都知道的原因,我朝人民的做法有点不一样:

下载 boot2docker 的镜像:建议直接去 boot2docker github 下载,如果还是很慢,可以考虑 V2EX 做的镜像,不过版本稍微有点低(1.3)。

然后将下载的 boot2docker.iso 复制到 ~/.boot2docker 文件夹里,这时候再执行 boot2docker init,会生成一段对应的密钥,同时打开 VirtualBox 会看到对应的虚拟机:

4. 启动 boot2docker 虚拟机

执行:boot2docker up

虚拟机对应的其他一些管理命令:

boot2docker {init|start|up|save|pause|stop|restart|status|info|delete|ssh|download}

截至这三步结束,其实我们只是完成了一件事:在 Mac 上为 docker 提供了 linux 的运行环境-boot2docker-vm.

疑问:

看很多文章说安装了 boot2docker 后,还需要安装 docker: brew install docker,在我的电脑上安装完 boot2docker 后就可以直接运行 docker -v,集成了?还是我之前安装过?


参考链接:

ma6174 commented 9 years ago

brew install docker 这是安装docker客户端,装上之后直接在mac的终端下就能操作虚拟机里面的docker,否则每次用都要ssh到虚拟机里面去执行docker命令

imsobear commented 9 years ago

@ma6174

通过 ssh 可以进到虚拟机,但是 docker client 连接会失败:

➜  deadlink git:(master) boot2docker up
Waiting for VM and Docker daemon to start...
.o
Started.
Writing /Users/guo/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/guo/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/guo/.boot2docker/certs/boot2docker-vm/key.pem
Your environment variables are already set correctly.

➜  deadlink git:(master) docker info
FATA[0032] An error occurred trying to connect: Get https://192.168.59.103:2376/v1.16/info: dial tcp 192.168.59.103:2376: i/o timeout
ma6174 commented 9 years ago
$ boot2docker up
Waiting for VM and Docker daemon to start...
......................oooo
Started.
Writing /Users/maweiwei/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/maweiwei/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/maweiwei/.boot2docker/certs/boot2docker-vm/key.pem
Your environment variables are already set correctly.

$ docker info
Containers: 49
Images: 61
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Dirs: 159
Execution Driver: native-0.2
Kernel Version: 3.16.7-tinycore64
Operating System: Boot2Docker 1.4.1 (TCL 5.4); master : 86f7ec8 - Tue Dec 16 23:11:29 UTC 2014
CPUs: 4
Total Memory: 1000 MiB
Name: boot2docker
ID: HTH4:F66V:RS76:XNDB:3VJR:3KDS:2USE:3LYK:FV2A:2KLF:4GEN:V23Q
Debug mode (server): true
Debug mode (client): false
Fds: 11
Goroutines: 11
EventsListeners: 0
Init Path: /usr/local/bin/docker
Docker Root Dir: /mnt/sda1/var/lib/docker

我这边是没问题的,怀疑是你docker服务没起来或者和虚拟机的网络有问题,可以这样排查一下:

  1. 登陆虚拟机内部执行docker info命令看是否正常
$ 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:~$ docker info
Containers: 49
Images: 61
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Dirs: 159
Execution Driver: native-0.2
Kernel Version: 3.16.7-tinycore64
Operating System: Boot2Docker 1.4.1 (TCL 5.4); master : 86f7ec8 - Tue Dec 16 23:11:29 UTC 2014
CPUs: 4
Total Memory: 1000 MiB
Name: boot2docker
ID: HTH4:F66V:RS76:XNDB:3VJR:3KDS:2USE:3LYK:FV2A:2KLF:4GEN:V23Q
Debug mode (server): true
Debug mode (client): false
Fds: 11
Goroutines: 11
EventsListeners: 0
Init Path: /usr/local/bin/docker
Docker Root Dir: /mnt/sda1/var/lib/docker
docker@boot2docker:~$ ps aux | grep docker
  585 root     /sbin/udhcpc -b -i eth0 -x hostname boot2docker -p /var/run/udhcpc.eth0.pid
  588 root     sshd: docker [priv]
  590 docker   sshd: docker@pts/0
  641 root     /sbin/udhcpc -b -i eth1 -x hostname boot2docker -p /var/run/udhcpc.eth1.pid
  682 root     /usr/local/bin/docker -d -D -g /var/lib/docker -H unix:// -H tcp://0.0.0.0:2376 --tlsverify --tlscacert=/var/lib/boot2docker/tls/ca.pem --tlscert=/var/lib/boot2docker/tls/server.pem --tlskey=/var/lib/boot2docker/tls/serverkey.pem
  766 docker   -sh
  810 docker   ps aux
  811 docker   grep docker

如果不能显示,说明虚拟机里面的docker没起来或者运行不正常。

主要是这个进程:682 root /usr/local/bin/docker -d -D -g /var/lib/docker -H unix:// -H tcp://0.0.0.0:2376 --tlsverify --tlscacert=/var/lib/boot2docker/tls/ca.pem --tlscert=/var/lib/boot2docker/tls/server.pem --tlskey=/var/lib/boot2docker/tls/serverkey.pem

如果docker里面服务正常,需要检查宿主机和虚拟机直接的网络是否正常

docker@boot2docker:~$ ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 08:00:27:FF:C5:9E
          inet addr:192.168.59.103  Bcast:192.168.59.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:feff:c59e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:44 errors:0 dropped:0 overruns:0 frame:0
          TX packets:27 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:9168 (8.9 KiB)  TX bytes:4546 (4.4 KiB)

docker@boot2docker:~$ exit

$ ping 192.168.59.103
PING 192.168.59.103 (192.168.59.103): 56 data bytes
64 bytes from 192.168.59.103: icmp_seq=0 ttl=64 time=19.057 ms
64 bytes from 192.168.59.103: icmp_seq=1 ttl=64 time=2.903 ms
64 bytes from 192.168.59.103: icmp_seq=2 ttl=64 time=0.892 ms
64 bytes from 192.168.59.103: icmp_seq=3 ttl=64 time=4.812 ms
64 bytes from 192.168.59.103: icmp_seq=4 ttl=64 time=10.279 ms
^C
--- 192.168.59.103 ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.892/7.589/19.057/6.531 ms

还有就是检查端口能否连上:

$ echo $DOCKER_HOST
tcp://192.168.59.103:2376
$ telnet 192.168.59.103 2376
Trying 192.168.59.103...
Connected to 192.168.59.103.
Escape character is '^]'.
^]
telnet> Connection closed.
imsobear commented 9 years ago

@ma6174 docker 是正常运行的:

docker@boot2docker:~$ docker info
Containers: 0
Images: 16
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Dirs: 16
Execution Driver: native-0.2
Kernel Version: 3.16.7-tinycore64
Operating System: Boot2Docker 1.4.1 (TCL 5.4); master : 86f7ec8 - Tue Dec 16 23:11:29 UTC 2014
CPUs: 4
Total Memory: 1.961 GiB
Name: boot2docker
ID: DVJK:4HVX:BTAJ:P4IU:BQNR:VCYW:3JWZ:TJMV:RUAD:VRHR:7ZCW:67Q6
Debug mode (server): true
Debug mode (client): false
Fds: 10
Goroutines: 11
EventsListeners: 0
Init Path: /usr/local/bin/docker
Docker Root Dir: /mnt/sda1/var/lib/docker

在我的宿主机器上 ping 不通:

➜  deadlink git:(master) ping 192.168.59.103
PING 192.168.59.103 (192.168.59.103): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
imsobear commented 9 years ago

@ma6174

搞定了,我猜测是 virtualbox 的版本问题,参考 docker-cant-connect-to-boot2docker-because-of-tcp-timeout :

➜  ~  ping 192.168.59.103
PING 192.168.59.103 (192.168.59.103): 56 data bytes
Request timeout for icmp_seq 0
➜  ~  sudo route -nv add -net 192.168.59 -interface vboxnet0
u: inet 192.168.59.0; u: link vboxnet0:a.0.27.0.0.0; RTM_ADD: Add Route: len 140, pid: 0, seq 1, errno 0, flags:<UP,STATIC>
locks:  inits:
sockaddrs: <DST,GATEWAY,NETMASK>
 192.168.59.0 vboxnet0:a.0.27.0.0.0 (0) 0 ffff ff
add net 192.168.59: gateway vboxnet0
➜  ~  ping 192.168.59.103
PING 192.168.59.103 (192.168.59.103): 56 data bytes
64 bytes from 192.168.59.103: icmp_seq=0 ttl=64 time=10.220 ms
...
➜  ~  docker ps
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
6a3faa45bca4        dockerfile/nodejs:latest   "node"              22 hours ago        Up 22 hours
ma6174 commented 9 years ago