0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

サクッとはじめるVagrant

Last updated at Posted at 2019-05-04

とりあえずVagrantでサクッとVMを立ち上げるのに必要な手順のまとめ。

必要なもの

  • VirtualBox
  • Vagrant

インストールに特に難しいことは無いと思うので省略。
インストーラぽちぽち。

大きいファイルを別のストレージに置きたい人はこれ

SSD + HDD構成なマシンでは大きいファイルをHDDに移したい人がほとんどだと思うので、以下の設定を入れておく。

VirtualBoxのデフォルトの仮想マシンフォルダの移動

ファイル -> 環境設定 -> 一般 -> デフォルトの仮想マシンフォルダーのパスを変更しておく。

環境変数VAGRANT_HOMEを設定

VAGRANT_HOMEに設定したパスにダウンロードしたboxが保存される。

起動したいVMイメージを探す

ここで起動したいOSのboxを探す。
Vagrant Cloud
今回はCentOS 7のboxを起動してみる。
image.png

Vagrantfileの生成

image.png
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使うとかだとネットワークの設定をいじる必要があるけど、それはまた別にまとめようかな…

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?