LoginSignup
16
18

More than 5 years have passed since last update.

初心者がVagrantを使ってみる。

Posted at

Vagrant(ベイグラント)とは

OSS(オープンソースソフトウェア)で、仮想環境上に仮想マシンを構築、利用できるようにセットアップを行うことができる開発環境自動構築ツールです。

つまり、一つ一つ設定を入力しないで、簡単に構築できる優れものです。
(私はさくらのVPSで作って壊して遊んでましたが時間がかかって大変でしたが、Vagrantは簡単で扱いやすいです。)

ただし、仮想マシンにNginxを自動でインストールさせたい場合などは、Ansible、Chef、シェルスクリプトなどといったツールを使います。

開発環境

OS X El capitan(v10,11,3)
HomeBrew導入済み

導入するもの

virtualbox(5.0.16)
vagrant(1.8.1)

ただし、virtualbox(5.0.16)が新しいためか、一部不具合が出る可能性があるでご了承ください。

ちなみにLinuxやWindowsも起動できますのでいいですね。

Vagrantで使う用語

Boxファイル

仮想マシンを起動するときにベースとなるイメージファイルです。

Vagrantfile

仮想マシンのスペックなど、仮想マシンの構成をこのファイルに記述します。

VirtualBoxとVagrantのインストール

brewを使いインストールします。

$ brew update
$ brew install Caskroom/cask/virtualbox Caskroom/cask/vagrant

インストールされている確認

$ vagrant -v
$ virtualbox

BOXファイルのインストール編

以下のコマンドでインストールできます。

$ vagrant box add BOX名 URL

BOX名

BOX名は自由に決められます。ただし、重複はできません。(同じ名前のBOXを2個以上作れません。)

URL

Boxファイルが配置されているURLまたはファイルパスを指定する。
Boxは自分で作成することができますが、公開されているものを使った方が楽なのでそれを使います。

URLは例えば、http://www.vagrantbox.es に書いてあるURLやChef社のhttps://atlas.hashicorp.com/bento/ などから指定できます。

Boxファイルインストール(centos7.1)

今回は前者のhttp://www.vagrantbox.es でCentOS 7.1をインストールしてみます。

時間がかかるのでご了承ください。

$ vagrant box add centos7.1 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.1/vagrant-centos-7.1.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7.1' (v0) for provider:
    box: Downloading: https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.1/vagrant-centos-7.1.box
==> box: Successfully added box 'centos7.1' (v0) for 'virtualbox'!

インストールされているboxの確認

$ vagrant box list
bento/centos-6.7   (parallels, 2.2.3)
bento/centos-6.7   (virtualbox, 2.2.3)
bento/centos-7.1   (parallels, 2.2.2)
bento/centos-7.1   (virtualbox, 2.2.2)
bento/centos-7.2   (parallels, 2.2.3)
bento/centos-7.2   (virtualbox, 2.2.3)
bento/ubuntu-14.04 (parallels, 2.2.3)
bento/ubuntu-14.04 (virtualbox, 2.2.3)
bento/ubuntu-15.04 (parallels, 2.2.3)
bento/ubuntu-15.04 (virtualbox, 2.2.3)
centos7.1          (virtualbox, 0)

私の場合はbentoでインストールしているものがありますが、初めてインストールしている場合は、centos7.1 (virtualbox, 0)だけが表示されています。

Boxファイルの削除

vagrant box remove BOX名

Vagrantfile作成編

まず作業用ディレクトリを作成、移動します。

$ mkdir ~/vagratsample
$ cd vagrantsample

Vagrant fileを生成するコマンド

$ vagrant init BOX名

今回は

$ vagrant init centos7.1
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.

vagrantfileが作成されているか確認

$ ls
Vagrant file

catvimlessコマンド等で内容を確認してみます。

less Vagrantfile

中身はRubyで書かれていますね。
ある行にconfig.vm.box = "centos7.1"と書かれていますね。

vagrantで仮想マシンの起動、停止、削除、確認

vagrant up(仮想マシン起動)

~/vagrantsampleに移動し

$ vagrant up

いろいろ表示されますが、処理が終了したら仮想マシンが起動しているか確認します。

$ vagrant status
running (virtualbox)

と表示されていれば起動しています。

他にはGUIでvirtualboxを起動し確認することができます。

vagrant ssh(SSH接続)

上で起動した場合、操作をするにはvirtualboxのGUIで操作しなければなりません。
そこでSSHで接続することでCUIで操作できるようにします。

仮想マシンが起動している状態で

$ vagrant ssh
Last login: Thu Mar 10 11:48:52 2016
[vagrant@localhost ~]$

接続できました。

SSH接続を抜け出すにはexitを使います。

[vagrant@localhost ~]$ exit
ログアウト
Connection to 127.0.0.1 closed.
$

ただし、仮想マシンは起動したままです。

vagrant halt(仮想マシン停止)

起動している状態で

$ vagrant halt
==> default: Attempting graceful shutdown of VM...

となり、停止します。

vagrant destroy(仮想マシン削除)

$ vagrant destory

とすれば削除されます。

vagrant status(仮想マシンの状態を確認)

現在、起動しているのか、停止しているのか、そもそも作成されているのか確認するコマンドです。

$ vagrant status

次書く内容は,Nginxなどをシェルスクリプトを使って導入する手順を書きたいと思います。

16
18
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
16
18