書き置き。

ほぼ勉強の記録みたいなもんです。

Vagrant で bridged を使いたいときは順序に要注意

vagrant で構築した環境でスマートフォン向けのサービスを作りたいのに、
hostonly ネットワークしか作成できなくて、同じネットワーク上にいる iPhoneから接続できなくて困ってた。

いろいろ調べた結果、困ってる人はたくさんいるようだったけど日本語情報はひとつも見つけられなかった。

Vagrantfile

Before

# Assign this VM to a host-only network IP, allowing you to access it
# via the IP. Host-only networks can talk to the host machine as well as
# any other machines on the same network, but cannot be accessed (through this
# network interface) by any external networks.
config.vm.network :hostonly, "192.168.30.11"

# Assign this VM to a bridged network, allowing you to connect directly to a
# network using the host's network device. This makes the VM appear as another
# physical device on your network.
# config.vm.network :bridged

After

config.vm.network :bridged
config.vm.network :hostonly, "192.168.30.11"

コメント部分を除いたこの2行、記述する順序が重要らしい。
この2行を bridged, hostonly の順で書いて vagrant reload、ifconfig を見てみると
確かに 我が家のDHCPで割り振られたIPアドレスを確認することができた。

vagrant init で生成された Vagrantfile にそんなハメがあるとは…。トホホ。

Mac 等では /etc/hosts ファイルに設定を書けば 自由なVirtualHost名を設定してアクセスできるけど、iPhoneではできない。
家庭内のDNSサーバーに設定をすることも可能だけど、外出時に使えないのは不便だ。
iPhoneから接続して動作確認をしなきゃいけないときは、VirtualHost 設定は使わないのが良いんだろうか。