とりあえずVagrantでサクッとVMを立ち上げるのに必要な手順のまとめ。
必要なもの
- VirtualBox
- Vagrant
インストールに特に難しいことは無いと思うので省略。
インストーラぽちぽち。
大きいファイルを別のストレージに置きたい人はこれ
SSD + HDD構成なマシンでは大きいファイルをHDDに移したい人がほとんどだと思うので、以下の設定を入れておく。
VirtualBoxのデフォルトの仮想マシンフォルダの移動
ファイル -> 環境設定 -> 一般 -> デフォルトの仮想マシンフォルダーのパスを変更しておく。
環境変数VAGRANT_HOME
を設定
VAGRANT_HOME
に設定したパスにダウンロードしたboxが保存される。
起動したいVMイメージを探す
ここで起動したいOSのboxを探す。
Vagrant Cloud
今回はCentOS 7のboxを起動してみる。
Vagrantfileの生成
boxのページを見ると「How to use this box with Vagrant:」の「New」ところにVM起動までのコマンドが載っているので、ひとまずvagrant init centos/7
の部分だけ実行し、Vagrantfileを生成する。
コマンドプロンプトやターミナル上で適当なディレクトリを作成し、そのディレクトリ内でvagrant init <box名>
を実行。
>vagrant init centos/7
==> vagrant: A new version of Vagrant is available: 2.2.0!
==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html
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が生成される。
Vagrantfileの編集
最低限ホストのスペックに合わせてVMに割り当てるリソースの調整くらいはしたいと思うので、生成されたVagrantfileの以下の部分のコメントアウトを外し、CPUコア数、メモリの割当を指定する。
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = false # true → falseに
# Customize the amount of memory on the VM:
vb.memory = "1024" # 割り当てたいメモリサイズ(MB)の指定
vb.cpus = "2" # 割り当てたいCPUコア数の指定
end
VM起動
ここまで設定できたら、あとはVagrantfileを設置しているディレクトリ内でvagrant up
を実行すると、初回はboxがダウンロードされ、VMが起動する。
>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
(略)
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
(略)
VM操作
起動したようなので早速触ってみる。そのまま同じディレクトリ内でvagrant ssh
を実行。
>vagrant ssh
Last login: Sat May 4 02:29:57 2019 from 10.0.2.2
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
ちゃんと動いてる。
VMのシャットダウン
VMから抜けてvagrant halt
でシャットダウンする。
>vagrant halt
==> default: Attempting graceful shutdown of VM...
VM内でshutdown -h now
とかしても問題なかったけど、Vagrant的にどうなんだろう…
VMとboxの削除
不要になって消す場合。
VM削除
そのまま同じディレクトリ内でvagrant destroy
を実行。
>vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...
いじったVMをまっさらにしたい場合も、vagrant destroy
した後にもう一度vagrant up
するのがいいかも。
box削除
boxもいらない場合は自分で消してあげる必要がある。まずはvagrant box list
で取得済みのboxの確認。
>vagrant box list
centos/7 (virtualbox, 1902.01)
ubuntu/xenial64 (virtualbox, 20181120.0.0)
そして削除。vagrant box remove <box名>
を実行。
>vagrant box remove centos/7
Removing box 'centos/7' (v1902.01) with provider 'virtualbox'...
確認。
>vagrant box list
ubuntu/xenial64 (virtualbox, 20181120.0.0)
ちゃんと消えたみたい。
ひとまずこれだけ覚えておけば、ちょっと使うくらいなら困らないはず。Webサーバ上げてホストからアクセスするとかDocker使うとかだとネットワークの設定をいじる必要があるけど、それはまた別にまとめようかな…