LoginSignup
28
27

More than 5 years have passed since last update.

Vagrant インストールから、仮想マシンの起動〜停止まで

Last updated at Posted at 2013-12-17

Vagrantってなんぞ?

  • 開発環境を自動で構築するコマンドラインツール
  • 仮想環境の上に、仮想マシンを構築して、それを利用する
  • chef 等のプロビジョニングツールを用いて、環境を自動的に構築できる

インストール

macだったら、dmg をダウンロードしてインストールする
あと、事前に Virtual Box をインストールしていること

$ wget https://dl.bintray.com/mitchellh/vagrant/Vagrant-1.4.0.dmg

動作確認

$ vagrant -v
Vagrant 1.4.0

Box を設定(CentOS)

$ wget http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130309.box

# vagrant box add name url
# name : box名(デフォルトは base)
# url : boxのURL
$ vagrant box add centos ~/Downloads/CentOS-6.4-x86_64-v20130309.box
Successfully added box 'centos' with provider 'virtualbox'!

$ vagrant box list
centos (virtualbox)

Vagrantfileの用意

$ cd ~/work
$ mkdir vagrant_test

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

$ ll
total 16
-rw-r--r--  1 kobayashi  staff  4606 12 11 17:35 Vagrantfile

$ vim Vagrantfile
  config.vm.box = "centos"  # 使用するboxを定義
  config.vm.provider :virtualbox do |vb| # guiモードで起動
    vb.gui = true
  end

boxの起動

$ vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[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] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.2.8
VirtualBox Version: 4.3
[default] Mounting shared folders...
[default] -- /vagrant

VirtualBoxのバージョンと、Guest Additionsのバージョンが一致しないので
問題が発生することも無くはないよ! と警告が出た

ステータスの確認

$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

サーバに接続してみる

$ vagrant ssh

[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS release 6.4 (Final)
[vagrant@localhost ~]$ exit

共有ディレクトリ

$ touch deleteme.txt
$ ll
total 16
-rw-r--r--  1 kobayashi  staff  4599 12 11 18:35 Vagrantfile
-rw-r--r--  1 kobayashi  staff     0 12 11 18:38 deleteme.txt

$ vagrant ssh

# Vagrantfile があるディレクトリ が、/vagrant/ にマウントされている
[vagrant@localhost ~]$ ll /vagrant/
total 8
-rw-r--r-- 1 vagrant vagrant    0 Dec 11  2013 deleteme.txt
-rw-r--r-- 1 vagrant vagrant 4599 Dec 11 09:35 Vagrantfile

サーバの停止

$ vagrant halt

[default] Attempting graceful shutdown of VM...

$ vagrant status
Current machine states:

default                   poweroff (virtualbox)

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

まずはここまで

用語

プロバイダ

仮想マシンを実行する仮想環境

  • VirtualBox、AWS、VMWareやら

プロビジョニング

仮想マシンにアプリのインストールやら設定を行うこと

  • shell、Puppet、chefやら

Boxファイル

仮想マシンを起動する際のベースとなる、イメージファイル
(ベースとなっているだけなので、起動後に環境をいじっても、Box ファイルには影響はない)

Vagrantfile

仮想マシンのスペックや、プロビジョニングツールを使った環境の設定手順等を書く所

参考

続き

28
27
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
27