LoginSignup
4
4

More than 5 years have passed since last update.

vagrantについて(メモ)

Last updated at Posted at 2015-08-12

一貫した開発環境を作るツールである。「私の環境では動く」バグにサヨナラしましょう~

プロジェクトのセットアップ

# プロジェクトディレクトリ作成
$ mkdir my_project
# プロジェクトディレクトリに移動
$ cd my_project
# カレントフォルダをルートディレクトリに指定
# Vagrantfile 生成
$ vagrant init 

$ vagrant init --minimalで実行すれば、最低限、コメント無しのVagrantfile が生成される。

Vagrantfile

Vagrantfileの主要機能は、プロジェクトに必要なマシンタイプ、そして、これらのマシンをどのように設定/プロビジョンするかを記述することです。
構文はRubyです。
Vagrantはカレントディレクトリから順番に上の階層へVagrantfileを探す。
複数Vagrantfileが有る場合、新しい設定は古い設定を上書きする。

基本のVagrantfile構造

Vagrant.configure("2") do |config|
  #...
end

"2"configのバージョンを指定する。設定可能なのは:1、2。

設定項目

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "CentOS"
  config.vm.hostname = "pc_name"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = 1024
    vb.cpus = 2
    vb.name = "vm_name"
  end
  config.vm.synced_folder "../helloWorld", "/var/www/html/helloWorld"
  config.vm.provision :shell, :path => "shellScript.sh"
end

BOX

vagrantではベースイメージのことをboxという。

boxのインストール

$ vagrant box add box_name box_url_or_imagePath

boxの使用

Vagrantfileを編集
rb
Vagrant.configure("2") do |config|
config.vm.box = "box_name"
end

online box list

boxの作成

下記のコマンドを使って、自分のBOXを作る。
* vagrant package
* vagrant repackage

起動とSSH接続

$ vagrant up
$ vagrant ssh

共有フォルダ

デフォルトでは、Vagrantfileが所在するフォルダは仮想マシンの /vagrantとして同期されている。

共有フォルダの指定

  • Vagrantfileに下記のように記述する rb config.vm.synced_folder "../helloWorld", "/var/www/html/helloWorld"
  • Apacheのwebパスを変更する bash $ rm -rf /var/www/html $ ln -fs /vagrant /var/www/html

保留/中断/破壊

  • vagrant suspend
  • vagrant halt
  • vagrant destroy

その他のコマンド

  • vagrant box list
  • vagrant box remove
  • vagrant provision
  • vagrant reload
  • vagrant resume
  • vagrant status
  • vagrant package

参考:
* http://lab.raqda.com/vagrant/index.html
* http://qiita.com/hiroyasu55/items/11a4c996b0c62450940f

4
4
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
4
4