おれさまラボ

実際に手を動かして理解を深めるブログ。

Vagrantをつかって仮想環境を構築する

気になっていたVagrantを使ってみる。

Vagrantは仮想環境の構築、コントロールを簡単、便利にしてくれるソフトウェアです。

 

1.まずは、仮想環境プラットフォームであるVirtualBoxをインストールします。

Downloads – Oracle VM VirtualBox

 

2.次に、Vagrantをインストールします。

Download Vagrant - Vagrant

 

3.コマンドプロンプトを起動します。

 

4.boxを追加します。

※boxとは仮想環境のテンプレートのようなもの。様々なboxが有志によって作られ、配布されています。今回は、A list of base boxes for Vagrant - Vagrantbox.esからCentOS-6.4-i386-v20130731.boxをダウンロードしています。

構文:varant box add {Name} {URL}

C:\Users\user>vagrant box add centos32 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-i386-v20130731.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos32' (v0) for provider:
    box: Downloading: http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6
.4-i386-v20130731.box
    box: Progress: 100% (Rate: 444k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos32' (v0) for 'virtualbox'!

 

5.ダウンロードしたOSを扱うためのディレクトリを準備します。

C:\Users\user>mkdir centos32
C:\Users\user>cd centos32

 

6.ダウンロードしたboxの初期設定を行います。

C:\Users\user\centos32>vagrant init centos32
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.

 上記のコメントが表示されれば完了です。

 

7.作成したフォルダにエクスプローラで移動し、Vagrantfileをテキストエディタで開き、IPアドレス設定のコメントアウトを解除します。

config.vm.network "private_network", ip: "192.168.33.10"

 

8.いよいよ、OSを起動します。

C:\Users\user\centos32>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos32'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos32_default_1451185241365_2724
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> 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 replace
    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 key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version o
f
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you
 see
    default: shared folder errors, please make sure the guest additions within t
he
    default: virtual machine match the version of VirtualBox you have installed
on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.2.16
    default: VirtualBox Version: 5.0
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/user/centos32
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

chown `id -u vagrant`:`id -g vagrant` /vagrant

Stdout from the command:



Stderr from the command:

chown: changing ownership of `/vagrant': Not a directory

 

9.vagrant sshと打つと、アクセス方法を教えてくれます。TeraTerm等のターミナルソフトからアクセスすればOKです。

コマンドプロンプトsshにデフォルトで対応していませんが、MacLinuxVagrant環境を構築した場合には、agrant sshでそのまま作ったOSにアクセスすることができます。

C:\Users\user\centos32>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:

Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: C:/Users/user/centos32/.vagrant/machines/default/virtualbox/private
_key

 

10.また、vagrant box listで登録しているboxが一覧表示できます。

C:\Users\user>vagrant box list
centos32 (virtualbox, 0)

 

11.仮想環境を削除したいときはremoveコマンドを利用します。

>vagrant box remove centos32

 

Vagrantではこのように簡単に仮想環境を作ったり、消したりできます。また、boxを自分で作ることができたり、起動時にコマンドを実行させたりできるので、自分の好きな環境を簡単にセットアップすることができるようになります。なんて便利!

 

# 2016/2/16追記

社内環境(proxy等の指定)によっては、boxのダウンロードがうまくいかないことがある。

C:\Users\19180>vagrant box add centos32 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-i386-v20130731.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos32' (v0) for provider:
    box: Downloading: http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-i386-v20130731.box
    box: Progress: 0% (Rate: 0curl:/s, Estimated time remaining: --:--:--)
An error occurred while downloading the remote file. The error message, if any, is reproduced below. Please fix this error and try again.

Failed to connect to developer.nrel.gov port 80: Timed out

そんな時は、boxを一旦ブラウザからダウンロードし、直接ファイルを指定してあげればOK。

C:\Users\User>vagrant box add centos32 C:\Users\User\Downloads\CentOS-6.4-i386-v20130731.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos32' (v0) for provider:
    box: Unpacking necessary files from: file://C:/Users/User/Downloads/CentOS-6.4-i386-v20130731.box
    box: Progress: 100% (Rate: 643M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos32' (v0) for 'virtualbox'!

 #2016/3/21追記

クリーンインストール後のWindows10ではまった話。以下のようなエラーをはいた場合は「Microsoft Visual C++ 2010 再頒布可能パッケージ」が足りていない模様。

>vagrant box add centos "https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box"
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos' (v0) for provider:
    box: Downloading: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
    box:
An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.

Microsoft Visual C++ 2010 再頒布可能パッケージ」は以下のリンクからダウンロード可能です。

Download Microsoft Visual C++ 2010 再頒布可能パッケージ (x86) from Official Microsoft Download Center

Download Microsoft Visual C++ 2010 再頒布可能パッケージ (x64) from Official Microsoft Download Center

 

参考

『MSVCR100.DLL が見つからなかったため、アプリケーションを開始できませんでした。』解決メモ - やすはるラボ+嫁(*・ω・)