つばろぐ

主に C#, .NET, Azure の備忘録です。たまに日記。

Vagrantの導入や使用についての備忘録

最近、話題になっている「Vagrant」を今更ながら導入してみました。
導入方法や参考にしたサイトについてメモしておきます。

Vagrantとは?

簡単に言うと、仮想マシンを作成するツールです。
ですがVMWareVirtualBoxとは違い、構築した環境を「Box」という形で保管し、配布することができます。
なお、Vagrantを動かすにはVirtualBoxが必要となりますので、予めインストールする必要があります。

公式ドキュメント

Vagrantのインストール

公式サイトのDownloadリンクからインストーラをダウンロードし、インストールします。
記事執筆時点の最新バージョンは「1.2.4」です。
ちなみに私のマシンには、バージョン「1.2.0」がインストールされています。

Boxの入手

VagrantはISOファイルから仮想マシンを作成するのではなく、Boxと言われる仮想マシンファイルを入手し、それを基に仮想マシンを作成します。
Boxの主な入手先は公式ドキュメントで紹介した、Vagrantbox.esです。

コマンドプロンプトを開き、以下のコマンドを入力します。

$ vagrant box add precise32 http://files.vagrantup.com/precise32.box
Downloading or copying the box...
[0KExtracting box...ate: 370k/s, Estimated time remaining: --:--:--)
Successfully added box 'precise32' with provider 'virtualbox'!

「precise32」とはBoxの名前です。好きな名前を付けることができます。また、Box名の後にはBoxをダウンロードするURLを記入します。

仮想マシンの初期化

$ cd /path/to/vm/directory
$ mkdir precise32_vm
$ cd precise32_vm
$ vagrant init precise32
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.

仮想マシンの起動

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'precise32'...
[0K[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[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 VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[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
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.2.0
VirtualBox Version: 4.1
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant

仮想マシンへの接続

$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2
vagrant@precise32:~$