参考
- 公式サイトのドキュメント
- Chef実践入門
手順
- Virtualboxのインストール
- Vagrantのインストール
- プロジェクトセットアップ
- Boxの生成
- 起動
- 停止
- 破棄
プロジェクトセットアップ
Vagrantのプロジェクトのセットアップはinitコマンドを使用する。実行するとVagrantfileが生成される。
$ mkdir vagrant_getting_started $ cd vagrant_getting_started/ $ vagrant init 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
Boxの生成
vagrant box addコマンドでVagrantで起動するOSのイメージを設定する。
https://vagrantcloud.com/discover/featured に既にサンプルを含めたイメージがいろいろあるので自分にあったものを探す。練習用であれば無難にhashicorp/precise32かhashicorp/precise64がよいかと。boxによっては対応する仮想環境が選択できるので途中で聞いてくるだろう。本記事ではvirtualboxを選択している。
#練習用 vagrant box add hashicorp/precise32 #例:ubuntu64bitを入れたい場合 vagrant box add chef/ubuntu-14.04
Vagrantにboxを追加したらそれをVagrantfileに記述する必要があるのでエディタで編集する。
$ vi Vagrantfile
編集はこちら
#config.vm.box = "base" config.vm.box = "chef/ubuntu-14.04"
box指定で初期化
initの引数にbox名を記述することでVagrantfileをあらかじめbox名が入った状態で初期化できる。
- Vagrantfileがあるとエラーになるのであらかじめ削除しておくこと。
vagrant init chef/ubuntu-14.04
起動
$ vagrant up vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'chef/ubuntu-14.04'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'chef/ubuntu-14.04' is up to date... ==> default: Setting the name of the VM: vagrant_getting_started_default_1406448329215_96618 ==> 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: Connection timeout. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => ~/vagrant_getting_started
接続確認
vagrant ssh Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64) * Documentation: https://help.ubuntu.com/ Last login: Sat Apr 19 05:27:17 2014 from 10.0.2.2 vagrant@vagrant:~$
接続できればOK。
停止
vagrantを停止する。次にupしたときはそれまでの設定が回復される。
$ vagrant halt ==> default: Attempting graceful shutdown of VM...
破棄
vagrantを破棄する。次にupしたときはすべて初期化された状態で起動する。登録済みのboxが削除される訳ではない。
$ vagrant destroy vagrant destroy default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Forcing shutdown of VM... ==> default: Destroying VM and associated drives...