fugafuga.write

日々のログ

Vagrant で開発環境構築 (Mac OSX)

テスト環境構築の手間とおさらばしたい方へ。






Vagrant とは

インスタントに仮想環境を作成・破棄できるツール。

VirtualBox などの仮想化ソフトと連携して動く。
Chef 、Puppet などの環境構築ソフトとも連携がとれる。
Box というOSのイメージファイルをスナップショットのように保存・配布できる。
配布された Box ファイルを利用すればすぐに構築済みの環境が利用できる。

VirtualBox のダウンロード&インストール

ここから取得してインストールしておく。
Downloads – Oracle VM VirtualBox

Vagrant をダウンロード&インストール

https://www.vagrantup.com

ダウンロードしたらインストール。

 % vagrant -v
Vagrant 1.7.2


Box ファイルのダウンロード&追加

Box ファイルとは、仮想環境のイメージファイルのようなもの。

以下にホスティングされているので、好きなもののURLをコピーしておく。
http://www.vagrantbox.es


以下のコマンドでBoxファイルをVagrantに登録する。

 % vagrant box add {Box名} {BoxファイルのURL または、Boxファイルへのパス}

上記で指定したBox名でVagrant に登録されます。

 % vagrant box add centos_65 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
==> box: Adding box 'centos_65' (v0) for provider:
    box: Downloading: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
==> box: Successfully added box 'centos_65' (v0) for 'virtualbox'!

vagrant box add は、Vagrant に対して box イメージを登録するだけです。
まだ、仮想環境は作成されていません。

登録された box を確認

% vagrant box list
centos_65 (virtualbox, 0)

vagrant box add で指定した Box名で登録されていることが確認できます。

仮想環境の起動設定ファイル作成

プロジェクトディレクトリを作る

% mkdir vagrant_test
% cd vagrant_test

Vagrantfile を作成する

% vagrant init centos_65

Vagrantfile とは、仮想環境の設定ファイルです。
今回は、centos_65 の box の仮想環境の設定ファイルが作成されます。

仮想環境を起動する

Vagrantfile が存在するディレクトリで以下のコマンドを実行

 % vagrant up

すると、仮想マシンの起動が開始されます。

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.6
    default: VirtualBox Version: 4.2
==> default: Mounting shared folders...
...

仮想環境に接続する

仮想環境の起動が完了したら、接続できるようになります。
簡単に接続できます。

 % vagrant ssh
Last login: Sat Feb 14 18:04:32 2015 from 10.0.2.2
[vagrant@vagrant-centos65 ~]$

ログイン直後は、vagrant というユーザーになっており、root には、パスワード:vagrant でログインすることが可能。

仮想環境をシャットダウンする

 % vagrant halt


仮想環境をサスペンドする

% vagrant suspend

サスペンドは、起動を素早くすることが可能ですが、ディスク容量とメモリを消費します。

仮想環境を破棄する

% vagrant destroy

仮想環境が破棄されます。
インストールしたものなど、仮想環境の中にあるものは全て削除されます。
Vagrantfile はそのまま残ります。

綺麗な環境が欲しい場合、再び、vagrant up を行うと仮想環境が起動します。

次やること

Chef と連携してテスト環境構築の自動化を図る。
自作 Box ファイルの作成。


参考
VagrantをMacで使ってみる - Qiita


実践 Vagrant

実践 Vagrant

Vagrant入門ガイド

Vagrant入門ガイド