はじめに
Vagrantとは、開発環境の構築と共有を簡単に行うためのツールです。
特徴
- Ruby製
- 仮想マシン環境をコードで管理し、コマンドで操作
- マルチプラットフォーム対応 (Windows/Mac/Linux)
- 複数のターゲット環境に対応 (VirtualBox/Hyper-V/Docker/VMware/AWS/Azure etc..)
- 作成した仮想環境イメージをBOX(テンプレート)として再配布可能
動作イメージ
よく使う用語
-
Box
Vagrantで利用できる環境のイメージ(Box検索) -
Vagrantfile
仮想マシンの設定を記述するファイル -
プロビジョン
初期構築処理。GuestOS作成時に自動的に実行される。
ハンズオン
目的
Vagrantを使って、VirtualBox上にGuestOSを作成する。
環境構築
- VirtualBoxをインストールする
- Vagrantをインストールする。
バージョンの確認
$ vagrant -v
Vagrant 2.0.2
Boxを追加する
# 追加済みのBoxの一覧を確認
$ vagrant box list
# Boxの追加
$ vagrant box add bento/centos-6
# 追加済みのBoxの一覧を確認
$ vagrant box list
bento/centos-6 (virtualbox, 201803.24.0)
Vagrantfileの作成
# 作業フォルダへ移動
$ cd /path/to/work/dir
# Vagrantfile作成
$ vagrant init bento/centos-6
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.
GuestOSの起動
# 起動
$ vagrant up
# →初回起動は少し時間がかかります。
# GuestOSへログイン
$ vagrant ssh
# →GuestOSの2222ポートにSSHで接続します。(ID/PASSはvagrant/vagrant)
# GuestOSからログアウト
$ exit
# GuestOSの一時停止
$ vagrant halt
# GuestOSの起動
$ vagrant up
# GuestOSの再起動
$ vagrant reload
# GuestOSの破棄
$ vagrant destroy
Vagrantfileの編集
プロビジョン用のスクリプトの記載
Vagrantfile
[省略]
Vagrant.configure("2") do |config|
[省略]
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
# ここから追記
config.vm.provision "shell", inline: <<-SHELL
yum install -y httpd
service httpd start
SHELL
# ここまで追記
end
# 仮想マシン起動
$ vagrant up
ポートフォワーディングの設定変更
Vagrantfile
[省略]
Vagrant.configure("2") do |config|
[省略]
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# ここから追記
config.vm.network "forwarded_port", guest: 80, host: 8080
# ここまで追記
[省略]
end
# 仮想マシン再起動
$ vagrant reload
ブラウザでhttp://localhost:8080
にアクセスし、
apacheの初期ページが表示されることを確認する。
プロビジョン用スクリプトの追記
Vagrantfile
[省略]
Vagrant.configure("2") do |config|
[省略]
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
config.vm.provision "shell", inline: <<-SHELL
yum install -y httpd
service httpd start
# ここから追記
yum install -y php
service httpd restart
# ここまで追記
SHELL
end
# プロビジョン再実行
$ vagrant provision
# GuestOSへログイン
$ vagrant ssh
# PHPバージョンの確認
$ php -v
PHP 5.3.3 (cli) (built: Mar 22 2017 12:27:09)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
# GuestOSからログアウト
$ exit
# GuestOSの破棄
$ vagrant destroy
まとめ
開発環境の構築、共有に手間を書けたくないと思いVagrantを学習しはじめました。
vagrantだけでは、プロビジョンの部分が弱いので、ChefやPuppet、Ansibleなどの構成管理ツールで補う必要があります。