Vagrantメモ
http://lab.raqda.com/vagrant/index.html
こちらを読んで、最低限利用するために必要な部分だけまとめた。
Vagrantとは
簡単な環境設定/複製可能なマシン/業界標準の技術で構築された1つの一貫したワークフローによって制御される可搬的な開発環境を提供してくれるツール。
ダウンロード:http://www.vagrantup.com/downloads.html
初期化
カレントディレクトリにvagrantfileを作成。
$ vagrant init
BOXのインストール
仮想マシンをすばやくクローンするためのベースイメージをBOXと呼ぶ。
$ vagrant box add precise32 http://files.vagrantup.com/precise32.box
HTTP経由でprecise32のboxをダウンロードし、precise32として保存する。 追加されたboxは複数のプロジェクトで再利用可能。
いろいろな仮想環境のboxはこちらでリストアップされている。
BOXをVagrantfileに適用する
Vagrant.configure("2") do |config| config.vm.box = "precise32" end
ここで「2」はVagrantのバージョンを定義しているもので、こちらで変更する必要はない。
# All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing.
仮想環境に接続
仮想環境を起動する
$ vagrant up
VirtualBoxを入れていない場合、以下のエラーで怒られるのでここで入れておく。
No usable default provider could be found for your system. Vagrant relies on interactions with 3rd party systems, known as "providers", to provide Vagrant with resources to run development environments. Examples are VirtualBox, VMware, Hyper-V. The easiest solution to this message is to install VirtualBox, which is available for free on all major platforms. If you believe you already have a provider available, make sure it is properly installed and configured. You can see more details about why a particular provider isn't working by forcing usage with `vagrant up --provider=PROVIDER`, which should give you a more specific error message for that particular provider.
仮想環境にSSH接続する
$ vagrant ssh
プロバイダ経由で起動する
$ vagrant up --provider=vmware_fusuion $ vagrant up --provider=aws
仮想環境で作成したファイルはどうなるのか
仮想環境での/vagrant
ディレクトリ配下が、Vagrantfileが格納されているカレントディレクトリと同期されるため、仮想環境上で/vagrant
配下にファイルを作成すると自動的にローカルに反映される。
仮想環境を止めるときは
- 保留:
vagrant suspend
仮想環境の現在の状態を保存し、停止する。復帰が早いものの、仮想環境はホストマシンのディスク容量を消費し、RAMの内容もディスクに追記するため容量を食う。 - 中断:
vagrant halt
仮想環境のOSをシャットダウンし、電源を切る。ディスク内容を保存しつつクリーンな状態で起動が行える。仮想環境によりホストマシンのディスク容量は消費したままとなる。 - 破壊:
vagrant destroy
仮想環境をすべて削除する。ホストマシンのディスク容量は、vagrant up
を行う前の状態に戻る。
Vagrantfileの便利な設定
プロビジョニング
vagrant up
した際に、ソフトウェアもインストールしてくれる機能。
ローカルでVagrantfileが格納されているディレクトリに、bootstrap.shを作成しておく。
Vagrant.configure("2") do |config| config.vm.box = "precise32" config.vm.provision :shell, :path => "bootstrap.sh" end
すでに起動しているboxに設定を反映させるにはvagrant reload
を実行すればOK。
ポートフォワーディング
ポートフォワーディングを行うには、以下のように設定する。
Vagrant.configure("2") do |config| config.vm.box = "precise32" config.vm.provision :shell, :path => "bootstrap.sh" config.vm.network :forwarded_port, host: 4567, guest: 80 end
これにより、http://127.0.0.1:4567
にアクセスするとWebページを見ることができる。
TODO
- AWS環境のboxを作るための手順を調べる
- chefとの連携方法
- 開発環境のプロビジョニングで必要なことを洗い出す