LoginSignup
38
42

More than 5 years have passed since last update.

Vagrant を使用して仮想マシンを作成してみる

Last updated at Posted at 2014-03-02

目的

  • Vagrant を使用して仮想マシンを作成してみる
  • 動作は Mac 上で確認

vagrant インストール

  • http://www.vagrantup.com/
    • ダウンロードから使用している OS のインストーラを取得し、インストールする(今回は Mac)
  • 仮想マシンを作成するツールが必要なので VirtualBox もインストールする

バージョン確認

  • ターミナルを開き、下記コマンドを入力
$ vagrant --version

Box(仮想マシン) の取得

$ vagrant box add “name” “url”
  • 取得した Box 一覧を表示する
$ vagrant box list
  • 他の Box イメージを取得するには

  • Box を削除するには

$ vagrant box remove
  • 今回は公式ページの通り、Box を追加しておく
$ vagrant box add precise32 http://files.vagrantup.com/precise32.box

仮想マシンの初期化

  • ディレクトリの作成
    • 仮想マシンごとにディレクトリを作る必要がある
$ mkdir UbuntuVM
  • 作成したディレクトリに移動し、初期化コマンドを打つ
$ vagrant init precise32
  • コマンドを打つと Vagrantfile というのが作成される
    • 中身は ruby で記載されていて、仮想マシンの設定ができる(今回はそのまま使用)

仮想マシンの起動

  • Vagrantfile のあるディレクトリで下記のコマンドを入力
$ vagrant up
  • Virtual Box を見ると、起動した VM が実行中になっていることがわかる vagrant_ubuntu.png

仮想マシンの停止・再起動

  • 仮想マシンの状態を見る場合は、下記のコマンドを入力する
$ vagrant status
  • 起動中であれば running となる

    default running (virtualbox)

  • 仮想マシンを停止するには、下記のコマンドを入力する

$ vagrant halt
  • 仮想マシンを再起動するには、下記のコマンドを入力する
$ vagrant reload
  • 仮想マシンを削除する場合は、下記のコマンドを入力する
    • Virtual Box 上からは削除されるが Vagrantfile は残る。
$ vagrant destroy

仮想マシンに接続・切断

  • 接続するには、下記コマンドを入力する
$ vagrant ssh
  • 接続できると以下が表示される

    Welcome to your Vagrant-built virtual machine.
    [vagrant@localhost ~]$

  • 切断には、下記コマンドを入力する

$ exit
38
42
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
38
42