Vagrantを使う(macOS High Sierra)

VM環境のセットアップをラクにするVagrantを入れてみる。
HashiCorp社 Vagrantの Getting Started のガイドを参考にVagrantを使えるようにセットアップをしてきます。

  1. インストール
    Vagrantのダウンロードページから、macOSを選択して vagrant_2.1.1_x86_64.dmg をダウンロード。
    https://www.vagrantup.com/downloads.html

Vagrant.pkgをダブルクリックしてインストールします。

Vagrant.pkg

インストール後、ターミナルで、vagrantコマンドが使えることを確認します。

$ vagrant -v
Vagrant 2.1.1
$ 
  1. Vagrantプロジェクトのセットアップ
     Vagrantプロジェクトの最初のステップとして、Vagrantfileを作成します。
     Vagrantfileは、Vagrantプロジェクトのroot directoryを指定すること、オプションの指定、仮想マシン、リソースに関する記述をします。
     Vagrantには、「vagrant init」コマンドがあり、Vagrant用にディレクトリを初期化し、Vagrantfileを作成します。
$ mkdir vagrant_getting_started
$ cd vagrant_getting_started/
$ 
$ vagrant init
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.
$
$ ls
Vagrantfile
$

このVagrantfileはテキストファイルですので、Gitなどバージョンコントロールシステムで管理することで、一意の環境構築ができるようになります。

  1. Boxesについて
     Vagrantでは、仮想マシンを一から作成するのではなく、boxesと呼ばれるベースイメージを使ってクイックに仮想マシンをクローンします。Vagrantfile作成後は、boxをインストールします。

Boxesは、「vagrant box add」コマンドで追加されます。このコマンドを実行することで、複数のVagrant環境で再利用が可能なように、特定の名前の下にboxが保管されます。

「vagrant box add hashicorp/precise64」を実行して、’hashicorp/precise64’配下にboxを追加します。
※仮想マシンを稼働する環境(providers)として、virtualboxを選択しました。
※「hashicorp/precise64」は、名前空間(namespace) hashicorp にある precise64 というboxを指定する表記です。
※「hashicorp/precise64」は、 HashiCorp’s Vagrant Cloud box catalog からダウンロードされます。

$ vagrant box add hashicorp/precise64
==> box: Loading metadata for box 'hashicorp/precise64'
    box: URL: https://vagrantcloud.com/hashicorp/precise64
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) virtualbox
3) vmware_fusion

Enter your choice: 2
==> box: Adding box 'hashicorp/precise64' (v1.1.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/hashicorp/boxes/precise64/versions/1.1.0/providers/virtualbox.box
==> box: Successfully added box 'hashicorp/precise64' (v1.1.0) for 'virtualbox'!
$
(後から解説予定)
Boxes are globally stored for the current user. Each project uses a box as an initial image to clone from, and never modifies the actual base image. This means that if you have two projects both using the hashicorp/precise64 box we just added, adding files in one guest machine will have no effect on the other machine.

In the above command, you will notice that boxes are namespaced. Boxes are broken down into two parts - the username and the box name - separated by a slash. In the example above, the username is "hashicorp", and the box is "precise64". You can also specify boxes via URLs or local file paths, but that will not be covered in the getting started guide.

作成者: naoya

IT業界(ネットワーク系)の管理職。2児のパパもやってます。

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

%d人のブロガーが「いいね」をつけました。
Verified by MonsterInsights