【Vagrant】 Vagrant + VirtualBox で仮想環境を構築
環境 Vagrant 1.8.1 VirtualBox 5.0.12
Vagrantを使った、基本的な仮想環境作成。
ホスト側の事前作業
- msysgit
- VirtualBox
- Vagrant
の最新版を公式からDLし、インストールしておく。
ベースとなるOSのイメージ『box』を追加
ベースとして様々なboxイメージが配布されているが、一番無難で安全そうなChef社のbento organizationからboxをダウンロードする。
【参考】 blog.chopschips.net
PS> vagrant box add bento/centos-7.1 ==> box: Loading metadata for box 'bento/centos-7.1' box: URL: https://atlas.hashicorp.com/bento/centos-7.1 This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) parallels 2) virtualbox 3) vmware_desktop Enter your choice: 2 ==> box: Adding box 'bento/centos-7.1' (v2.2.2) for provider: virtualbox box: Downloading: https://atlas.hashicorp.com/bento/boxes/centos-7.1/versions/2.2.2/providers/virtualbox.box box: Progress: 100% (Rate: 1465k/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'bento/centos-7.1' (v2.2.2) for 'virtualbox'!
途中で
1) parallels 2) virtualbox 3) vmware_desktop Enter your choice: 2
と聞かれるので、VirualBoxを選択する。
これでイメージがダウンロードされる。
現在のbox一覧を確認したいときは
PS> vagrant box list bento/centos-7.1 (virtualbox, 2.2.2)
Windows上のディレクトリ
Vagrantのboxファイルなどは
C:\Users\<ユーザー名>\.vagrant.d
にある
boxのリネーム
boxのリネームは、実ファイルをリネームするだけ。
vagrantコマンドで表示されるものも変更される。
Windowsだとboxのあるディレクトリは
C:\Users\<ユーザー名>\.vagrant.d\boxes\
この下にあるboxファイルをリネームすればよい。
Vagrantfileを作成
ホスト側のVMを作りたいディレクトリに移動。
ここではC:/Users/<ユーザー名>/VirtualBox VMs/CentOS7.x
とする
以下のコマンドを実行。
vagrantを実行するための設定ファイル、Vagrantfileが作成される。
PS> vagrant init <box名> 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起動
Vagrantfileを作ったディレクトリで以下のコマンドを実行すると、VMが自動で作成される。
PS> vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Checking if box 'bento/centos-7.1' is up to date... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Running 'pre-boot' VM customizations... ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes.. default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => C:/Users/<ユーザー名>/VirtualBox VMs/CentOS7.x
VMに接続する
sshで接続できる。
デフォルトでは
IP 127.0.0.1 post 2222
ID、PASSはデフォルト
ID vagrant PASS vagrant
になっている。
vagrantを終了する
ホスト側で以下のコマンドを実行する。
PS> vagrant halt ==> default: Attempting graceful shutdown of VM...