【Vagrant】インストールと基本コマンド
環境
- OS X 10.10.5
- Vagrant 1.8.1
- VirtualBox 5.0.16 r105871
用語
プロバイダ
Virtual Boxとか、VM Wareとか仮想マシン本体のこと。 EC2なんかもプロバイダとして利用出来るらしい。
プロビジョニング
Chefとか。 ミドルウェアのインストールや設定を行うツール。 シェルスクリプトでも出来るみたいで、Chefを使う気満々だったのが少し削がれた。
Boxファイル
仮想マシンを起動する際のベースとなるイメージファイル。 通常、OSイメージをベースにvagrantユーザの作成・sshd起動・プロビジョニングツールのインストールなど最小限の設定を行う。
Vagrantfile
仮想マシンのスペックやプロビジョニングツールの指定などの構成を記述するファイル。 Rubyで記述する。 こいつさえあれば、どんな環境でも同じ仮想マシンが構築できる。
Vagrantインストール
公式サイトからインストーラを入手、ポチポチしてインストールします。
バージョン確認
$ vagrant -v
Vagrant 1.8.1
ヘルプ
$ vagrant -h Usage: vagrant [options] <command> [<args>] -v, --version Print the version and exit. -h, --help Print this help. Common commands: box manages boxes: installation, removal, etc. connect connect to a remotely shared Vagrant environment destroy stops and deletes all traces of the vagrant machine global-status outputs status Vagrant environments for this user halt stops the vagrant machine help shows the help for a subcommand init initializes a new Vagrant environment by creating a Vagrantfile login log in to HashiCorp's Atlas package packages a running vagrant environment into a box plugin manages plugins: install, uninstall, update, etc. port displays information about guest port mappings powershell connects to machine via powershell remoting provision provisions the vagrant machine push deploys code in this environment to a configured destination rdp connects to machine via RDP reload restarts vagrant machine, loads new Vagrantfile configuration resume resume a suspended vagrant machine share share your Vagrant environment with anyone in the world snapshot manages snapshots: saving, restoring, etc. ssh connects to machine via SSH ssh-config outputs OpenSSH valid configuration to connect to the machine status outputs status of the vagrant machine suspend suspends the machine up starts and provisions the vagrant environment version prints current and latest Vagrant version For help on any individual command run `vagrant COMMAND -h` Additional subcommands are available, but are either more advanced or not commonly used. To see all subcommands, run the command `vagrant list-commands`. # vagrant [subcmd] -h でサブコマンドのヘルプも参照できる $ vagrant box -h Usage: vagrant box <subcommand> [<args>] Available subcommands: add list outdated remove repackage update For help on any individual subcommand run `vagrant box <subcommand> -h`
Boxファイルのインストール
Boxファイルは自分で作成可能ですが、vagrantbox.esで配布もされています。
自作は必要ができたら取り組むとして今回はvagrantboxから入手します。
Boxファイルの追加
# vagrant box add [box名(任意)] [URL or PATH] $ vagrant box add Ubuntu14.04_daily_Cloud_Image_amd64 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'Ubuntu14.04_daily_Cloud_Image_amd64' (v0) for provider: box: Downloading: https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box ==> box: Successfully added box 'Ubuntu14.04_daily_Cloud_Image_amd64' (v0) for 'virtualbox'! # 確認 $ vagrant box list Ubuntu14.04_daily_Cloud_Image_amd64 (virtualbox, 0)
Boxファイルの削除
# vagrant box remove [box名]
$ vagrant box remove Ubuntu14.04_daily_Cloud_Image_amd64
Vagrantfileの作成
Vagrantfileは"vagrant init [box名]"を叩くと作成されます。
# vagrant init [box名] $ vagrant init Ubuntu14.04_daily_Cloud_Image_amd64 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
vagrant init で作成されたvagrantファイルにはデフォルトで色々書かれているので、それを参考に設定を記述してみた。
ちなみに今回設定していることは以下
Vagrantfile
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure(2) do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://atlas.hashicorp.com/search. config.vm.box = "Ubuntu14.04_daily_Cloud_Image_amd64" # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine ### (1)仮想マシンをGUIで起動する(ウィンドウを立ち上げる) vb.gui = true # # Customize the amount of memory on the VM: # vb.memory = "1024" end # # View the documentation for the provider you are using for more # information on available options. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies # such as FTP and Heroku are also available. See the documentation at # https://docs.vagrantup.com/v2/push/atlas.html for more information. # config.push.define "atlas" do |push| # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" # end # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. ### (2)起動時にパッケージの更新とデスクトップのインストールをする ### プロビジョニングは初回のvagrant upでしか走らないので注意 config.vm.provision "shell", inline: <<-SHELL sudo apt-get update sudo apt-get install -y ubuntu-desktop SHELL end
仮想マシンの起動と停止
起動
vagrantで仮想マシンを起動するには、Vagrantfileがあるディレクトリでvagrant upを叩きます。
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'Ubuntu14.04_daily_Cloud_Image_amd64'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: Ubuntu1404_daily_Cloud_Image_amd64_default_1458397444446_89786 ==> default: Clearing any previously set forwarded ports... ==> 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: Warning: Remote connection disconnect. 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: 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.36 default: VirtualBox Version: 5.0 ==> default: Mounting shared folders... default: /vagrant => /Users/hoge/Documents/vagrant/Ubuntu14.04_daily_Cloud_Image_amd64
Vagrantファイルにて"vb.gui = true"と設定しているので、VirtualBoxのウィンドウで立ち上がる。
初期ユーザは
となってます。
停止
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
廃棄
"vagrant destroy"を叩くと、仮想マシンを廃棄できます。
プロビジョニングの設定などは初期起動時のみしか読み込まれないので、Vagrantfileを書き直した時などは一度仮想マシンを廃棄したほうが良いです。
$ vagrant destroy default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Destroying VM and associated drives...