2014.09.15 追記
最新のバージョンでは、デフォルトでホストオンリーアダプターが追加になったようです。このため、本記事のような作業は不要となりました。
ホストオンリーアダプターの IPアドレスは、以下のコマンドで確認できます。
boot2docker ip
ref: https://github.com/boot2docker/boot2docker#container-port-redirection
Container Port redirection
The latest version of boot2docker sets up two network adaptors, one using NAT to allow the VM to download images and files from the internet, and a host only network that Docker container's ports will be exposed on.
boot2docker で作成される VM は、NAT アダプタしか追加されないため、MAC 上からは、直接アクセスできない。
このため、VM <-> MAC 間の通信が可能な ホストオンリーアダプターを追加してみる。
追加
-
VM(boot2docker) が起動していれば、停止。いなければ作成
boot2docker stop or boot2docker init
-
ホストオンリーネットワーク(10.10.10.0/24)を新規に追加する。
hostonlyif="vboxnet$(VBoxManage list hostonlyifs|grep -c ^Name)" VBoxManage hostonlyif create VBoxManage hostonlyif ipconfig $hostonlyif --ip 10.10.10.1 --netmask 255.255.255.0
- 上記を実行すると MAC 上に 10.10.10.1 の IP が振られる
-
作成したホストオンリーネットワークに DHCP を追加
VBoxManage dhcpserver add --ifname $hostonlyif --ip 10.10.10.2 --netmask 255.255.255.0 --lowerip 10.10.10.10 --upperip 10.10.10.10 --enable
- lowerip と upperip を同じにしているので、VM(boot2docker) に振られる IPアドレスは
10.10.10.10
となる -
VBoxManage: error: DHCP server already exists
となった場合は、すでに DHCPサーバが存在しているので、上記コマンドのadd
をmodify
に変更して実行する。
- lowerip と upperip を同じにしているので、VM(boot2docker) に振られる IPアドレスは
-
VM に上記ネットワークに属する NIC を追加
VBoxManage modifyvm boot2docker-vm --nic2 hostonly --nictype2 virtio --hostonlyadapter2 $hostonlyif
削除
-
作成状況の確認
VBoxManage list hostonlyifs VBoxManage list dhcpservers
-
VM からホストオンリーアダプターを削除
VBoxManage modifyvm boot2docker-vm --nic2 none
-
DHCP と ホストオンリーネットワークの削除
VBoxManage dhcpserver remove --netname HostInterfaceNetworking-$hostonlyif VBoxManage hostonlyif remove $hostonlyif