せてぃーずノート

Javaのイベント参加レポートとかを書いたりします。

Windowsでvagrantを試してみる

参考にするのはこのページ

仮想環境構築ツール「Vagrant」で開発環境を仮想マシン上に自動作成する - さくらのナレッジ

本当はdockerを試そうと思っていました。
でもdockerのチュートリアルをやろうとしたら、最初にvagrantを入れろって言うので仕方なく入れます。

って思っていたのですが、vagrantもすげー面白い!
むしろこれ極めればdockerいらないんじゃない?とか思っちゃうかも

vagrantのインストール

vagrantにはWindows向けのインストーラーがちゃんとあります。
プログラムファイルには出てきませんが、パスも通してくれるのでインストールすればすぐに使えます。
再起動は必要ですけどね。

最初の仮想マシン作成

で、早速initしてみる。

d:\vm>vagrant init centos-6-x64 http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210.box
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

【てきとーな訳】
Vagrantfileがこのディレクトリが完成した
ただちにvagrant upで最初のVM環境を作ってみたまえ!

VagrantfileにはインストールするOSの種類や仮想マシンの環境とかを定義します。
詳しいことはここを読むといいっぽい。

Vagrantfile - Vagrant Documentation

言われたとおりにvagrant upを実行すると、OSダウンロードして仮想マシンを作成。 その後起動までしてくれます。
OSのダウンロードは最初の1回だけでいいそうです。

d:\vm>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Box 'centos-6-x64' was not found. Fetching box from specified URL for
the provider 'virtualbox'. Note that if the URL does not have
a box for this provider, you should interrupt Vagrant now and add
the box yourself. Otherwise Vagrant will attempt to download the
full box prior to discovering this error.
Downloading box from URL: http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x
64-vbox4210.box
Extracting box...ate: 2300k/s, Estimated time remaining: --:--:--)
Successfully added box 'centos-6-x64' with provider 'virtualbox'!
[default] Importing base box 'centos-6-x64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
DL is deprecated, please use Fiddle
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
prevent things such as shared folders from working properly. If you see
shared folder errors, please make sure the guest additions within the
virtual machine match the version of VirtualBox you have installed on
your host and reload your VM.

Guest Additions Version: 4.2.10
VirtualBox Version: 4.3
[default] Mounting shared folders...
[default] -- /vagrant

VirtualBoxを開いてみると仮想マシンが作成されて動いています。やったね。

今日のまとめ

ここまでの所要時間DL時間含めて5分かからずです。
これが自分で作った場合、isoを落としてVMにOS入れて初期設定して・・・と倍の時間はかかるでしょう。
目的にもよりますが、テスト環境を構築したいときなんかvagrantのほうが断然おすすめと思われます。