Vagrant environment

Working with Virtual environment

Starting up Vavgrant ( Need to have setup a VM software like VB )

vagrant init

Basics

# add a box
vagrant box add ubuntu/trusty64
# check existing box
vagrant box list
# update a box
vagrant box update --box box_name
# remove a box
vagrant box remove box_name

VagrantFile setup

Vagrant.configure(“2”) do |config|
config.vm.box = “ubuntu/trusty64” # base image of box
config.vm.box_version = “1.1.0”. # box version
config.vm.box_url = “https://….” # the source url of box

Install soft through provisioning

#1 Configure through shell
Vagrant.configure("2") do |config|
    config.vm.provision "shell" do |s|
  # config.vm.provision provision_name, type: “shell” do |s|
        s.variable = value
    end
end
# Configure docker
Vagrant.confgure("2") do |config|
    config.vm.provision "docker" do |docker|
        docker.build_image "/vagrant/app"  # build image from folder
        docker.pull_iamges "ubuntu" # build image pulling from hub
        docker.run container_name
    end
end
#2 Configure through shell , by providing the file
Vagrant.configure("2") do |config|
    config.vm.provision :shell, path: "bootstrap.sh"
end# Configure docker

And have the bootstrap install the files on start of the setup
./bootstrap.sh
apt-get update
apt-get install -y apache
if ! [ -L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant /var/www
fi

Deploy Box

vagrant init hashicor/precise32 || vagrant init hashicorp/bionic64

Can discover more boxes here – BoxLand

 

Using the VM

# create an vm
# initialize a vm
vagrant init testFolder. 
# the folder that contain vagrant file# start a vm
vagrant up
# stop a vm
vagrant stop
# reload vm
vagrant reload

 

 

Other Config stuff

Networking:

  1. by configuring forwarded ports, you can post your host machine’s data to a port on gust machine.
  2. by configuring private network, you can access your guest machine that is not accessible to public
Vagrant.configure("2") do |config|
    config.vm.network  "forwarded_port", guest:5000, host:5001
    config.vm.network "private_port", ip:0.0.0.1

Synced folder: Synced a folder on the host machine to the guest machine.

Vagrant.configure("2") do |config|
    config.vm.synced_folder "/app" "/vagrant" # host to guest folder
end

Configure SSH: configure how vagrant can access to your vm using ssh

Vagrant.configure("2") do |config|
    config.ssh.private_key_path = key_path
    config.ssh.username = user_name
    config.ssh.password = password
    config.ssh.forward_agent = True

By setting forward_agent to True, we can use local SSH keys instead of leaving keys sitting on your server.

Was this article helpful?

Related Articles

Leave A Comment?