概要
vagrantの環境のboxを保存しておくことで、バックアップとして利用したり別のローカル環境にて同じvagrant環境を用意することができる。
手順
現在のvagrant環境からboxファイルを作成する。
VM nameを調べる。以下の場合は "default" がVM nameとなる。
$ 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`.
以下のコマンドでboxファイルを出力
$ vagrant package [:VN_name] --output [:output_file_name].box
実行
$ vagrant package default --output ubuntu_trusty64_test.box
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: /Users/user_name/ubuntu_trusty64/ubuntu_trusty64_test.box
/Users/user_name/ubuntu_trusty64/ubuntu_trusty64_test.box
が出力され、バックアップが完了。
出力したboxファイルを使用してvagrant環境を構築する
vagrant用ディレクトリを作成
$ mkdir ubuntu_trusty64_test2
$ cd ubuntu_trusty64_test2
outputしたboxファイルを持ってくる
$ pwd
/Users/user_name/ubuntu_trusty64_test2
$ cp /Users/user_name/ubuntu_trusty64/ubuntu_trusty64_test.box .
$ ls -l
total 1018968
-rw-r--r-- 1 yoshinomasayoshi staff 521710999 5 11 17:46 ubuntu_trusty64_test.box
vagrant box addする。コマンドは
$ vagrant box add [:VM name] [:box file path]
実行
$ vagrant box add ubuntu_trusty64_test2 ubuntu_trusty64_test.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu_trusty64_test2' (v0) for provider:
box: Unpacking necessary files from: file:///Users/user_name/ubuntu_trusty64_test2/ubuntu_trusty64_test.box
==> box: Successfully added box 'ubuntu_trusty64_test2' (v0) for 'virtualbox'!
確認
$ vagrant box list
ubuntu/trusty64 (virtualbox, 20190429.0.0)
ubuntu_trusty64_test2 (virtualbox, 0)
追加されているので、vagrantにてboxを利用する
$ vagrant init ubuntu_trusty64_test2
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.
構築完了。あとはvagrant upでいつも通り起動可能。
$ vagrant up
参考
http://vdeep.net/vagrant-package
ありがとうございますmm