LoginSignup
1
10

More than 5 years have passed since last update.

Vagrant + Virtualboxで仮想環境構築

Last updated at Posted at 2017-03-25

※2016年11月に、別のブログで書いた記事を移行したものです。


Vagrantは、VirtualBoxと合わせて、コマンドだけで簡単に仮想環境を立ち上げたり潰したりすることができるツールです。
今回は、Windows上でCentOS7の環境を構築したいと思います。コマンドプロンプトを使えば良いですが、ここではGit Bushを使っています。

VirtualBoxのインストール

Downloads – Oracle VM VirtualBox

Vagrantのインストール

Download - Vagrant by HashiCorp

Boxのダウンロード

Boxファイルとは、仮想環境の元になるファイルのこと。
https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.boxから、好きなOSをダウンロードする。
今回は、CentOS 7.2を選択する。
Git Bush(もしくはコマンドプロンプトなど)を立ち上げ、以下のコマンドを実行する。

$ vagrant box add CentOS72 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'CentOS72' (v0) for provider:
    box: Downloading: https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box
    box:
==> box: Successfully added box 'CentOS72' (v0) for 'virtualbox'!

以下のコマンドで確認できる。

$ vagrant box list
CentOS72 (virtualbox, 0)

仮想マシンの起動

好きなディレクトリに移動し、Vagrantfile(Vagrantの設定ファイル)を作成する。
好みの問題だが、プロジェクトごとに環境を用意すると管理しやすいので、"c\Project\プロジェクト名"などのディレクトリを作り、その中でVagrantfileを作成すると良い。

$ cd /c/Projects
$ mkdir [プロジェクト名]
$ cd [プロジェクト名]
$ vagrant init CentOS72
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が作成されている

$ dir
Vagrantfile

仮想マシン起動する

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'CentOS72'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: gtd_console_default_1478779101548_74836
==> 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: 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 of
    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 the
    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.3.30
    default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
    default: /vagrant => C:/Projects/gtd_console

Git Bashの場合、以下のコマンドでログインできる。コマンドプロンプトの場合、SSH接続は対応していないので、TeraTermなどを使ってログインする。

$ vagrant ssh
[vagrant@localhost ~]$

Vagrantfileの設定

仮想マシンの設定をする。

ポートを開ける

例えば、Railsなら3000、Jenkinsなら8080を使う。
以下を、Vagrantfileに追記

config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.network "forwarded_port", guest: 8080, host: 8080

コメントアウトされている部分を参考にできる。

# config.vm.network "forwarded_port", guest: 80, host: 8080

メモリとCPU数を指定

以下の部分のコメントアウトを外して、仮想マシンにメモリを割り当てる。

  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end

  config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
    vb.memory = "2048"
    vb.cpus = 2
  end

参考
Configuration - VirtualBox Provider - Vagrant by HashiCorp

共有フォルダを指定

以下をコメントアウトして設定することで、ゲストOSとホストOSでフォルダを共有することができる。

  # config.vm.synced_folder "../data", "/vagrant_data"

今回は、以下のように指定する。

  config.vm.synced_folder "./data", "/home/vagrant/data"

CentOS7のタイムゾーン設定

$ timedatectl list-timezones | grep tokyo
$ timedatectl list-timezones | grep Tokyo
Asia/Tokyo
$ sudo timedatectl set-timezone Asia/Tokyo
$ timedatectl
      Local time: Thu 2016-11-17 23:33:07 JST
  Universal time: Thu 2016-11-17 14:33:07 UTC
        RTC time: Thu 2016-11-17 23:33:07
       Time zone: Asia/Tokyo (JST, +0900)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: yes

参考
Windows上でVirtualBox+Vagrant+CentOSによる仮想環境構築 - Qiita

補足

VirtualBoxを見てみると
プロジェクト名_default_1478779101548_74836 という名前で仮想マシンができている。
Vagrantが、VirtualBoxでの仮想マシンの作成・起動・停止などを代行してくれているということだろう。

1
10
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
1
10