最低限知っておくべきVagrantの設定とコマンドを紹介するよ!
とりあえずVagrantの設定を始められるように準備。
study1で作成したファイルを使用するってのでもOK。
$ mkdir study2
$ cd study2
$ vagrant init precise64 http://files.vagrantup.com/precise64.box
$ vagrant upちなみに、Vagrantfileのコメントが邪魔くさい人は、↓をコピペして、自力でVagrantfileを作ってもらってもOKだ。
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
endまぁ、よくある書き方だ。
$ vagrant statusするとこんな感じで表示されるはずだ!
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.running (virtualbox) となってるので、現在快調に動作中だよ!
一時停止。メモリの状態も含めて復元可能。
$ vagrant suspend確認してみよう。
$ vagrant status
Current machine states:
default saved (virtualbox)
To resume this VM, simply run `vagrant up`.最もディスク容量が必要。ゲストマシンのハードディスク+RAM領域分。
シャットダウン。再開時は通常のブートプロセスを経ることになる。
$ vagrant halt確認してみよう。
$ vagrant status
Current machine states:
default poweroff (virtualbox)
The VM is powered off. To restart the VM, simply run `vagrant up`正常終了しなかった場合、強制終了も可能。物理的に電源切るのと同じ。
$ vagrant halt --forceそれなりにディスク容量必要。ゲストマシンのハードディスク領域分。
$ vagrant destroy
Are you sure you want to destroy the 'default' VM? [y/N] ydestroy時には本当に実行していいか聞いてくる。気にせず y と入力しよう。
確認してみよう。
Current machine states:
default not created (virtualbox)
The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.いちいち確認されるのがウザければ
$ vagrant destroy --forceで、有無を言わさず破棄できる。
本当に全部消しちゃうのでディスク容量的には優しいが、再開時は全部最初からやり直すため時間がかかる。
初回起動時と同じでOK。
$ vagrant up紹介済みだけど改めて。
$ vagrant sshVagrantfileを修正したら実行するコマンド。
$ vagrant reloadVagrantfile修正して終わった気になったのに、反映されねぇぞ(゚Д゚)ゴルァ!って時には大抵このコマンドを叩き忘れてる。
ShellやChefで記述されたプロビジョニングを実行。
$ vagrant provisionChefのレシピを作成する時など、何度もプロビジョニングを実行する場合、このコマンドを使おう。
すでに学んだ通り、Vagrantの設定は Vagrantfile に記述するよ!
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Setting Here!
endちなみに、Vagrantfileがあるディレクトリをプロジェクトディレクトリって言ったりするから覚えておこう。
デフォルトでは、プロジェクトディレクトリを、仮想マシンの /vagrant ディレクトリと共有する。
まずはホスト側で適当にファイルを作って、仮想マシンにログインして確認してみよう。
この例では現時刻をファイルに書き込んで、それを共有してみた。
$ date > synced_file
$ ls
Vagrantfile synced_file
$ cat synced_file
2014年 3月 7日 金曜日 08時26分33秒 JST
$ vagrant ssh
vagrant@precise64:~$ ls /vagrant/
synced_file Vagrantfile
vagrant@precise64:~$ cat /vagrant/synced_file
2014年 3月 7日 金曜日 08時26分33秒 JST
vagrant@precise64:~$ exit確かに共有されてるね!
もちろん、任意のディレクトリに設定を変更可能。
まずは下ごしらえ。
$ mkdir host_dir
$ echo "hello, guest" > host_dir/from_host.txt次にVagrantfileを適当なテキストエディタで開いて
config.vm.synced_folder "host_dir/", "/guest_dir"を追記。じゃあ、設定を反映して、確認してみよう。
$ vagrant reload
$ vagrant ssh
vagrant@precise64:/$ ls /
bin dev guest_dir initrd.img lib64 media opt root sbin srv tmp vagrant vmlinuz
boot etc home lib lost+found mnt proc run selinux sys usr var
vagrant@precise64:/$ ls /guest_dir/
from_host.txt
vagrant@precise64:/guest_dir$ cat /guest_dir/from_host.txt
hello, guestもちろん、ゲスト側からホスト側へもファイルは共有できる。
vagrant@precise64:~$ echo "hello, host" > /guest_dir/from_guest.txt
vagrant@precise64:~$ ls /guest_dir/
from_guest.txt from_host.txt
vagrant@precise64:~$ exit
$ ls host_dir
from_guest.txt from_host.txt
$ cat host_dir/from_guest.txt
hello, hostこの機能を使えば、ソースの修正はいつも使ってるエディタで。
動作の確認は仮想マシンで、なーんてのも楽勝だよね。
さて、2点ほど注意点。
- ホスト側のディレクトリは作成済みであること
- ゲスト側のディレクトリは絶対パスのみ記述可能
これを守らないと、Vagrantさんは激おこなのだ!o(*>д<)o″))
ネットワーク設定も簡単!簡単!
config.vm.network :forwarded_port, guest: 80, host: 8080これで、ゲストの80ポートをホストの8080ポートに、フォワードしてくれる。
確認してみよう。
$ vagrant reload
$ echo "Hello, Vagrant." > index.html
$ vagrant ssh
vagrant@precise64:~$ cd /vagrant
vagrant@precise64:~/vagrant$ sudo python -m SimpleHTTPServer 80
Serving HTTP on 0.0.0.0 port 80 ...ホスト側でブラウザを立ち上げて、http://localhost:8080/を開いてみよう。
「Hello, Vagrant.」って表示されれば完璧だ!
ネットワークの設定は後で、もう少し詳しい内容をやるよ。
じゃあ、最後に「 Ctrl+C 」でHTTPサーバを終了して後始末をしておこう。
vagrant@precise64:~/vagrant$ exit
$ vagrant destroy --force