Vagrant has a wide variety of commands available to you, to list all the commands available use the following command from your terminal.
vagrant list-commands
The most common commands that you might use are the following.
Command | Description |
---|---|
up | Startups a machine |
init | Create vagrant file based on url |
provision | Provisions a machine |
reload | Restarts a machine |
halt | Turns off a machine |
destroy | Destroys a machine |
ssh | Logging via SSH |
status | Shows status of a machine |
Now that you are familiar with Vagrant it’s time to create a Vagrant file that will store all of the configuration needed for your virtual machine.
First you must select a virtual machine that you will want to init your file with, you can find a list of available virtual machines at Atlas. For this example I will be using vivid64, Atlas will provide you with the init command.
Before you run the command, make sure that your located in the folder where you want save your files that will be used in your application.
mkdir ~/Desktop/app
cd ~/Desktop/app
vagrant init ubuntu/vivid64
Result: Now you will have a file named Vagrantfile
.
➜ app vagrant init ubuntu/vivid64
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.
Now that you have created the Vagrantfile
it’s time to startup the machine. If it’s your first time running this command it could take sometime to complete, due to Vagrant going out and downloading, and configuring all of the files that it requires.
vagrant up
Result
➜ app vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'ubuntu/vivid64' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/vivid64'
default: URL: https://atlas.hashicorp.com/ubuntu/vivid64
==> default: Adding box 'ubuntu/vivid64' (v20151211.0.0) for provider: virtualbox
default: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/vivid64/versions/20151211.0.0/providers/virtualbox.box
==> default: Successfully added box 'ubuntu/vivid64' (v20151211.0.0) for 'virtualbox'!
==> default: Importing base box 'ubuntu/vivid64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/vivid64' is up to date...
==> default: Setting the name of the VM: app_default_1450318941149_36154
==> 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 => 2222 (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: Connection timeout. 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.34
default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
default: /vagrant => /Users/rick/Desktop/app
Now the machine is created and ready to be used over an SSH connection.
vagrant ssh
If you open VirtualBox, you will see the new machine created.
Not much different then doing it manually, but now we have a shared directory from the local machine and the virtual machine.
When you finish working, you can always shutoff the machine by using the following command
vagrant halt
Result
➜ app vagrant halt
==> default: Attempting graceful shutdown of VM...
The last part is to configure the Vagrantfile
for your specific requirements I.E IP address, port to forward, name, and default shared folder.
Open up the Vagrantfile
file and uncomment and alter the following lines.
config.vm.network "forwarded_port", guest: 80, host: 3000
config.vm.network "private_network", ip: "192.168.56.102"
Now if you startup the machine you can find it at 192.168.56.102
and the port 3000 being forward. If you would like to change the folder that is being shared you can do this by starting the machine and creating the folder that you want to share.
vagrant up
vagrant ssh
mkdir /var/www
chown -R vagrant:vagrant /var/www
exit
vagrant halt
Open up the Vagrantfile
file and uncomment and alter the following lines.
config.vm.synced_folder "./", "/var/www"
That’s it the /var/www
folder is now being shared. Vagrant has many different functionalities available for you to use. I encourage you to look at the documentation if you are trying to accomplish a very specific task.
I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More