LoginSignup
3
4

More than 5 years have passed since last update.

Vagrantを使った環境構築

Posted at

Vagrantを使った環境構築

1 VirtualBox のダウンロード&インストール

2 Vagrant をダウンロード&インストール

https://www.vagrantup.com

  • ダウンロードしたらインストール。
 % vagrant -v
Vagrant 1.7.2

3 Box ファイルのダウンロード&追加

  • Box ファイルとは、仮想環境のイメージファイルのようなもの。
  • 以下にホスティングされているので、好きなもののURLをコピーしておく。
  • Discover Vagrant Boxes - ATLAS

  • 以下のコマンドでBoxファイルをVagrantに登録する。

# vagrant box add {Box名} {BoxファイルのURL または、Boxファイルへのパス}
vagrant box add centos67_customized "file://~/OneDrive/vagrant/centos67_customized/centos67_customized.box"
  • 登録されたboxの確認
vagrant box list

4 仮想環境の設定

  • 仮想環境の設定はVagrantfileを通して行います。
  • プロジェクトディレクトリを作成し、Vagrantfileを作成します。
  • ==VagrantfileはDockerfileのようなもので、このファイルが有ればvagrant initを行う必要はない。==
mkdir ~/OneDrive/vagrant
mkdir ~/OneDrive/vagrant/rails_development
cd ~/OneDrive/vagrant/rails_development
vagrant init centos67_customized
vi Vagrantfile
  config.vm.network :public_network, :ip => "192.168.1.200", :netmask => "255.255.255.0", :bridge => "en0: Wi-Fi (AirPort)"
  config.vm.hostname = "dev"
  config.vm.network :private_network, ip:"192.168.3.200"
  • SSHの設定
config.ssh.forward_agent = true
  • 共有フォルダの設定
  • config.vm.synced_folder “ローカルのパス”, “仮想環境上のパス”
config.vm.synced_folder "/Users/soushi/OneDrive/railsapp", "/railsapp", type: "nfs"
  • vmメモリとCPUコアの設定(VirtualBoxの例)
config.vm.provider "virtualbox" do |vb|
    # Customize the amount of memory and cpu cores on the VM:
    vb.customize ["modifyvm", :id, "--memory", "5120", "--cpus", "8", "--ioapic", "on"]
end
  • 完成したVagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.network :public_network, :ip => "192.168.1.200", :netmask => "255.255.255.0", :bridge => "en0: Wi-Fi (AirPort)"
  config.vm.network :private_network, ip:"192.168.3.200"
  config.vm.hostname = "dev"
  config.ssh.forward_agent = true
  config.vm.synced_folder "/Users/soushi/OneDrive/railsapp", "/railsapp", type: "nfs"
  config.vm.box = "centos67_customized"
  config.vm.box_url = "file://~/OneDrive/vagrant/centos67_customized/centos67_customized.box"
  config.vm.provider "virtualbox" do |vb|
    # Customize the amount of memory and cpu cores on the VM:
    vb.customize ["modifyvm", :id, "--memory", "5120", "--cpus", "8", "--ioapic", "on"]
  end
end

5 仮想環境を起動する

  • Vagrantfileが存在するディレクトリで以下を実行
vagrant up

6 仮想環境に接続する

vagrant ssh

7 Boxファイルを自作する

http://te2u.hatenablog.jp/entry/2015/05/11/012225を参考

参考

3
4
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
3
4