Vagrant Quick Start on Windows 10


What's vagrant?

Vagrant can build and manage virtual machine environment in a single workflow.

Vagrant Architecture


Requirement

  • vagrant_2.0.2_x86_64.msi
  • VirtualBox-5.2.6-120293-Win.exe

Steps

  1. Install vagrant . Then reboot the system.
  2. type "vagrant" in the cmd prompt
    >vagrant
    Usage: vagrant [options] <command> [<args>]
  3. Project Setup
    c:\>mkdir vagrant_getting_started
    c:\>cd vagrant_getting_startedc:\vagrant_getting_started>vagrant initA `Vagrantfile` has been placed in this directory.
  4. A box is a operating system in Vagrant. You can download the OS from HashiCorp's Vagrant Cloud box catalog. I will use this box ubuntu/trusty64.
    c:\vagrant_getting_started>vagrant box add ubuntu/trusty64==> box: Loading metadata for box 'ubuntu/trusty64'
  5. Using a box
    c:\vagrant_getting_started>vim Vagrantfile
  6. Change the text from config.vm.box = "base" to config.vm.box = "ubuntu/trusty64". For more options you can reference this guide
  7. Type the following command to get a virtual machine running Ubuntu.
    c:\vagrant_getting_started>vagrant up
    No usable default provider could be found for your system.
  8. The no provider message means you don't have a virtualbox on your machine. You can download and install it from Virtualbox download page.
  9. After installed Virtualbox.
    c:\vagrant_getting_started>vagrant up
    Bringing machine 'default' up with 'virtualbox' provider..
    ==> default: Importing base box 'ubuntu/trusty64'...
  10. Interact the new virtual machine with ssh . It will show a password prompt. Type vagrant to pass the prompt.
    c:\vagrant_getting_started>vagrant ssh
    vagrant@127.0.0.1's password:
    Welcome to Ubuntu
  11. We can log out with the logout command.
    vagrant@vagrant-ubuntu-trusty-64:~$ logout
  12. local machine folder(c:\vagrant_getting_started) will sync to virtual machine folder( /vagrant). Let's prove it.
    > vagrant ssh
    $ ls /vagrant
    $ touch /vagrant/foo
    $ exit
    > dir/w
  13. Install Apache to the virtual machine via the shell script. Edit a shell script and name it as bootstrap.sh
    ~
    #!/usr/bin/env bash
    apt-get update
    apt-get install -y apache2
    if ! [ -L /var/www ]; then
      rm -rf /var/www
      ln -fs /vagrant /var/www
    fi
    ~
  14. Edit the Vagrantfile to start the shell script.
    ~
      config.vm.box = "hashicorp/precise64"
      config.vm.provision :shell, path: "bootstrap.sh"
    ~
  15. If the guest machine is already running, run vagrant reload --provision to provision it.
    If the guest machine is not running, run  vagrant up to provision it.
  16. Check Apache status
    > vagrant ssh
    :~$ service apache2 status
     * apache2 is running
  17. Add port forwarding to the Vagrantfile file.
    config.vm.box = "hashicorp/precise64"
      config.vm.provision :shell, path: "bootstrap.sh"
      config.vm.network :forwarded_port, guest: 80, host: 4567
  18. Run step 15 to take effect.
  19. Now you can go to the url http://127.0.0.1:4567 from your browser.
  20. To use vagrant share you have to install something:
    1. > vagrant plugin install vagrant-share
    2. install ngrok driver.If the executale 'ngrok' not found, append the location to the PATH variable.
  21. Virtual machine control functions
    FunctionsProsConsCallRecall
    suspendingmore disk spacefast restore your workvagrant suspend to save the current statevagrant upto resume
    haltingtake some time to startpreserving the contents of diskvagrant halt to shut downvagrant upto boot
    destroyingtake some time to startremoving all traces of the guest machinevagrant destroy to shut downvagrant upto work
  22. Rebuild your project
    c:\vagrant_getting_started>vagrant up

Troubleshoot

1. >vagrant up
The machine index which stores all required information about
running Vagrant environments has become corrupt. This is usually
caused by external tampering of the Vagrant data folder.

Vagrant cannot manage any Vagrant environments if the index is
corrupt. Please attempt to manually correct it. If you are unable
to manually correct it, then remove the data file at the path below.
This will leave all existing Vagrant environments "orphaned" and
they'll have to be destroyed manually.

Path: C:/Users/Username/.vagrant.d/data/machine-index/index
Remove index and index.lock files under the path C:/Users/Username/.vagrant.d/data/machine-index/

2.>vagrant up
The VirtualBox VM was created with a user that doesn't match the current user running Vagrant. VirtualBox requires that the same user be used to manage the VM that was created. Please re-run Vagrant with that user. This is not a Vagrant issue.

The UID used to create the VM was: 0 Your UID is: 501
 Just delete .vagrant directory under your project folder.

Reference

Comments

Popular posts from this blog

How to write data into a excel file using vbscript

Format date as yyyy-mm-dd using vbscript