LoginSignup
45
44

More than 5 years have passed since last update.

Vagrant を導入する

Last updated at Posted at 2013-06-28

WEB+DB PRESS vol.75 でも紹介されていた Vagrant を遅ればせながら導入しました。
Vagrant の導入記事なんて巷に溢れていますが、記事が古かったりしてかなり振り回されたので自分でもまとめてみることにしました。

ポイント

  • gem を使わず公式のパッケージからインストールする
  • VirtualBox を 4.2.14 にアップグレードしてはいけない
    • 2013-07-09 追記:VirtualBox 4.2.16 がリリースされました参考)。VirtualBox 4.2.16 + Vagrant 1.2.2 の組み合わせで正常に動作することを確認しました。

Vagrant とは?

Vagrant は仮想マシンの立ち上げを自動化するコマンドラインツールです。
元々は Oracle VirtualBox 向けのツールだったようですが、現在では VMWare でも使えるようになったみたいです。

インストール

公式のインストールパッケージを使いましょう。

2013年6月現在 Vagrant の最新バージョンは 1.2.2 ですが、導入記事によくある gem install vagrant で導入した場合古い 1.0.7 がインストールされてしまいました。

古いバージョンを使用すると、この後の手順でダウンロードする box ファイルに互換性がないため長時間待った挙げ句に失敗して何も残らないという悲惨な目に遭います(遭いました)。

また gem を使うために OS X + Homebrew 環境で ruby を導入したのですがその状態で gem install vagrant しても vagrant コマンドにパスが通らず、別途作業が必要になるなど非常に手間が掛かります。

box を追加する

Vagrant では新しく仮想マシンを作成する際、雛形となる box と呼ばれるものが必要となり、あらかじめローカルマシンに box が追加されていなくてはなりません。
これらの box は様々なOS・バージョン・構成のものが公開されており、その一覧も http://www.vagrantbox.es/ で見ることができます。

box を追加するためのコマンドは次の通り。box名は好きに指定できます。

$ vagrant box add <box名> <url>

今回は VirtualBox 向けの CentOS 6.4 x86_64 Minimal を導入してみます。
なお各種 box ファイルは小さいものでも約300MB程度あるので、なるべく高速な通信環境で行う事をオススメします。

$ vagrant box add centos-6.4-x86_64 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box
Downloading or copying the box...
Extracting box...te: 684k/s, Estimated time remaining: 0:00:01))
Successfully added box 'centos-64-x86_64' with provider 'virtualbox'!

$ vagrant box list
centos-64-x86_64 (virtualbox)

Vagrantfile を作成する

次のコマンドを実行するとカレントディレクトリに Vagrantfile という名前のファイルが作成されます。仮想マシンが作成されるわけではありません。

$ vagrant init centos-64-x86_64
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 には「どんな仮想マシンを設定するのか」といった設定情報が Ruby で記述されています。
仮想マシンの作成や起動はこの Vagrantfile のあるディレクトリに移動してから実行することになります。

仮想マシンを起動する

仮想マシンを起動したい場合は Vagrantfile のあるディレクトリに移動してから次のコマンドを実行します。
初回に実行した場合のみ自動で env_1234567890 みたいな名前の仮想マシンが作成されます。

Vagrantfile を特に編集しない場合ネットワークアダプタは NAT に設定され、SSH接続を行うためにゲストの22番ポートからホストの2222番ポートへのフォワーディングが設定されます。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos-64-x86_64'...
[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] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant

仮想マシンにSSHで接続する

無事に仮想マシンが起動したら次のコマンドでSSH接続することができます。

デフォルトの状態では Vagrantfile を設置したディレクトリが VirtualBox の共有フォルダー機能を使用して /vagrant にマウントされています。Vagrantfile を編集することで共有フォルダを変更・追加することもできそうです。

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.

[vagrant@localhost ~]$ pwd
/home/vagrant

[vagrant@localhost ~]$ ls -l /vagrant
合計 8
-rw-r--r-- 1 vagrant vagrant 4359  6月 27 09:01 2013 Vagrantfile

[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

仮想マシンを停止する

仮想マシンを停止する場合は次のいずれかのコマンドを実行します。

  • vagrant suspend …… 仮想マシンの状態を保存して終了します。
  • vagrant halt …… 仮想マシンをシャットダウンします。
  • vagrant destroy …… 仮想マシンを強制終了し、破棄(削除)します。

参考サイト

45
44
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
44