LoginSignup
28
34

More than 5 years have passed since last update.

Vagrantで開発環境を手軽に構築し壊す。

Last updated at Posted at 2014-05-10

Vagrantを試したものの忘れてしまいそうなのでメモっておきました。

Vagrantを利用すると簡単にできること。

  • ローカルPCに商用環境と同じ開発環境を手軽に準備。
  • OS(WindowsやMacなど)に依存しない開発環境を準備。
  • 手軽に仮想開発環境をたちあげる。
  • VagrantはChefやシェルなどと連携して自動で環境のセットアップする。
  • 利用しなくなった開発環境はすぐ廃棄する。
  • 構築した環境は共有する。
  • プロジェクト単位で異なる設定の開発環境を簡単にセットアップする。

Vagrantを利用して仮想環境が立ち上がるまでは大きくは以下の流れになります。

1.VirtualBoxをインストール。
2.Vagrantのインストール。
3.Box(※1)のダウンロード。
4.Vagrantの初期化。(vagrant init コマンド)
5.環境の起動。(vagrant upコマンド)

(※1)〜環境を立ち上げる際のベースイメージとなるファイル、Vagrantの世界ではBoxと呼ぶ。Vagrantが仮想環境を構築する際はBoxを用い利用可能な仮想マシンを素早く立ち上げる。

上述の流れで試していきます。VagrantはVirtual Boxの利用が前提ではありませんが、親和性が非常に高いということで、Virtual Boxを利用します。

試したバージョン。

  • Mac OS X Marverics (10.9.2)
  • Vagrant 1.3.5
  • VirtualBox 4.3.8

VirtualBoxをインストール。

Virtual BoxはMacなら .dmg形式、Windowsの場合は .exe形式で配布されているため容易にインストールできます。

筆者はVirtual Boxから VirtualBox 4.3.8 for OS X hostsをダウンロードしインストール。

Vagrantをインストールする。

Virtual Boxのインストールと同様に .dmg形式で配布されているので、Macの通常のインストール方法で容易にインストールできます。

Vagrant-1.3.5.dmg をダウンロードし、インストール。

Boxの追加。

BoxはVagrantbox.esというサイトで有志により公開されているものがあるので、ありがたく利用しました。

今回はVagrantbox.esで公開されているBoxのなかのCentOS6.5のBoxを追加します。

$  vagrant box add centos65 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box

Downloading or copying the box...
Successfully added box 'centos65' with provider 'virtualbox'!

これでcentos65という名前のboxが追加されているはずなので確認します。。

$ vagrant box list
centos65 (virtualbox)

追加されていることが確認できました。

Vagrantの初期化。

Vagrantはプロジェクト単位に独立な仮想環境構築します。初期化にはvagrant initコマンドを実行する。(例えば、同じCentOS6.5を利用した環境ではLAMP環境の場合もあれば、Java+NoSQLの環境+MQなど様々な構成がプロジェクト毎に異なるはず)
Vagrant initを実行するとカレントディレクトリにVagrantfileという、環境を構築する設定ファイルが生成されます。

先ほど追加したCentOS6.5のBox、centos65を用い初期化します。プロジェクト単位にディレクトリを作成しそこへ移動してから初期化をします。

初期化する。
$ vagrant init centos65

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.
$ ls
Vagrantfile

カレントディレクトリにVagrantfileが作成され初期化できました。

Vagrant環境の起動〜廃棄。

実際に初期化した、Vagrantを起動してみます。

起動する。
$ vagrant up

ringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos65'...
[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 machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] Mounting shared folders...
[default] -- /vagrant

これで、centos65のBoxをテンプレートとしたVagrant環境作成され、起動しました。

なおVagrant環境の実体が、$ cd /Users/<ユーザ名>/VirtualBox\ VMsに作成されています。

試しにVagrant環境へアクセスしてみます。実際のアクセスにはSSHを利用しますが、vagrant sshで直接Vagrant環境へ接続できます。

Vagrant環境へSSHする。
$ vagrant ssh
Last login: Sat May 10 07:18:38 2014 from 10.0.2.2
[vagrant@vagrant-centos65 ~]$

[vagrant@vagrant-centos65 ~]$ cat /etc/redhat-release
CentOS release 6.5 (Final)
[vagrant@vagrant-centos65 ~]$ exit
logout
Connection to 127.0.0.1 closed.

ここまでで、CentOS6.5の仮想環境が構築され、無事にVagrant環境へのアクセスができました。
Vagrantの環境をシャットダウンしてみます。

シャットダウンする。
$ vagrant halt
[default] Attempting graceful shutdown of VM...

Vagrant環境の状態を確認する。

ステータスを確認する。
$ vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

パワー・オフpoweroffになっていることがわかります。
最後に、作成した環境を廃棄します。

Vagrant環境を廃棄する。
$ vagrant destroy
Are you sure you want to destroy the 'default' VM? [y/N] y
[default] Destroying VM and associated drives...

Vagrant環境が廃棄されました。/Users/<ユーザ名>/VirtualBox\ VMsから仮想環境の実体が削除されていることが確認できます。
仮想環境が廃棄されても、Vagrant環境生成のベースとなるBoxが廃棄されたわけではないので、またvagrant upすればすぐにまっさらな環境を作ることができます。

というわけで、vagrantをで環境の立ち上げから、廃棄までの一連の流れをためしてみました。
別のエントリで、ネットワークの設定、自動で設定を行う方法をまとめていこうと思います^^

利用したvagrantコマンド。

起動する。
$ vagrant up
シャットダウンする。
$ vagrant halt
ステータスを確認する。
$ vagrant status
登録されているBox一覧を表示する。
$ vagrant box list
Vagrant環境を廃棄する。
$ vagrant destroy
Vagrant環境にSSHする。
$ vagrant ssh
28
34
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
28
34