7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vagrant入門

Posted at

Vagrantの使い方を簡単にご紹介します。
Vagrantはboxと呼ばれる仮装マシンのテンプレートを元に仮装環境を簡単に構築できます。
コマンドラインで操作するので、説明するのが楽です(?)
※筆者の環境はMac OS Xです

前回の記事

Vagrant Box をイチから作る!(備忘録)

対象

  • Vagrant初心者

事前準備

  • VirtualBoxがインストール済み
  • Vagrantがインストール済み
  • Vagrant Boxの用意

VirtualBoxのインストール

Vagrantのインストール

Vagrant Boxの用意

  • 自分で作成したもの
  • この辺からダウンロードしてきてもOK

手順

Vagrant Boxの追加

  • Macのターミナルを立ち上げる
  • Vagrant Boxの追加
$ vagrant add [box名] [boxのパス]
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box '[box名]' (v0) for provider: 
    box: Unpacking necessary files from: file:///[boxのパス]
==> box: Successfully added box '[box名]' (v0) for 'virtualbox'!

# $ vagrant add centos6 /vagrant/box/centos6_ja.box
  • 追加されたかどうか確認
$ vagrant box list

Vagrantの起動

  • Vagrant初期化
$ 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.

# box名は省略可能ですが、後の設定項目が1つ増えます
  • Vagrantfile(設定)の編集
$ vi Vagrantfile
Vagrantfile
# 使用するBox名
config.vm.box = "[box名]"
# ポートフォワーディング(http://localhost:[ポート番号]/でアクセスしたい場合等に)
config.vm.network "forwarded_port", guest: 80, host: [ポート番号]
# 仮装マシンのIPアドレス
config.vm.network "private_network", ip: "192.168.xxx.xxx"
# GUIの有効化やメモリ数
config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "512"
end
  • Vagrant起動
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box '[box名]'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: [box名]_default_xxxxxxxxxxxxx_xxxxx
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (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: Warning: Connection timeout. Retrying...
    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: Mounting shared folders...
    default: /vagrant => /vagrant/[box名]
  • Vagrantにアクセス
$ vagrant ssh

Vagrantの停止

  • 電源をOFFする
$ vagrant halt
==> default: Attempting graceful shutdown of VM...

Vagrantの削除

$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

# -fオプションでyes/noを聞かれずに削除します
# また`vagrant up`すると仮装環境を作れます

コマンドリスト(一部抜粋)

コマンド 説明
vagrant init 初期化
vagrant ssh vagrantにアクセス
vagrant up 起動
vagrant halt 停止
vagrant reload 再起動
vagrant destroy 削除
vagrant status ステータス
vagrant global-status 全ステータス
vagrant box add [box名] [boxパス] vagrant box追加
vagrant box remove [box名] vagrant box削除
vagrant box list vagrant boxリスト

複数のVagrantを動かす場合

  • 下記のようなファイル構造にしておくと分かりやすいです
/vagrant
    /box
        - box1
        - box2
    /仮装1
        - Vagrantfile
    /仮装2
        - Vagrantfile
    ・・・
7
7
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
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?