Vagrant
Share resources between host and guest machines
In this tutorial, you will configure your Vagrant environment to share resources between your host and guest machines. You will set up port forwarding to access services running on the guest machine, synchronize files to simplify development, and verify the changes.
Sharing resources between the host and guest machines enable you to develop locally on your host machine while running services on your guest machine, streamlining the development workflow.
Configure port forwarding
Port forwarding lets you access services running on the guest machine directly from the host machine. Update your Vagrantfile
to forward the ports for the Terramino demo application.
Tip
You can find the complete configuration for these tutorials in the Learn Vagrant Get Started GitHub repository. The final configuration for this specific tutorial is in the 03.Vagrantfile
file.
Vagrantfile
# Forward ports for Terramino (8081 for frontend, 8080 for backend)
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 8081, host: 8081
These configurations map the guest machine's ports to the host machine, enabling access to the Terramino backend on port 8080
and frontend on port 8081
.
Reload your guest machine to apply the updated configuration.
$ vagrant reload --provision
The vagrant reload
command restarts the virtual machine and applies the updated Vagrantfile
configuration. This ensures the forwarded ports are correctly mapped between the host and guest machines.
Visit localhost:8081
in your browser to play Terramino.
Enable folder synchronization
Folder synchronization creates a shared directory between your host and guest machines, ensuring changes made locally are reflected in the virtual machine in real-time.
Update your Vagrantfile
to synchronize the ./terramino-go
directory on your host machine with the /home/vagrant/terramino-go
directory on the guest machine.
Vagrantfile
# Sync the terramino-go directory
config.vm.synced_folder "./terramino-go", "/home/vagrant/terramino-go", create: true
Reload the box to enable folder synchronization.
$ vagrant reload --provision
The synced folder establishes a bidirectional link between the host and guest directories. Changes in one location are mirrored in the other while the virtual machine is running. If you destroy the virtual machine with vagrant destroy
, the local directory remains untouched, serving as the source of truth.
When you recreate the virtual machine using vagrant up
, Vagrant immediately mounts the synced folder, copying all local files to the guest machine.
If you were to delete your local directory, Vagrant would remove the corresponding synced folder in the guest machine. However, any files created directory within the VM's directory (outside of the synced folder) would remain unaffected. In this case, deleting your local directory and restarting the VM would prompt the provisioning scripts to detect the missing Git directory and clone a new copy.
Modify and verify files
Now that synchronization is enabled, make a change on your host machine and verify it in the guest machine.
On the host machine, open terramino-go/main.go
and add the following health check handler.
terramino-go/main.go
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
Your local main.go
file should look like the following.
terramino-go/main.go
// Set up HTTP routes
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Terramino - HashiCorp Demo App\nhttps://developer.hashicorp.com/\n"))
})
http.HandleFunc("/env", envHandler)
http.HandleFunc("/redis", server.redisHandler)
http.HandleFunc("/score", server.highScoreManager.HandleHTTP)
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
Verify that this change is reflected in the guest machine.
$ vagrant ssh -c "cat /home/vagrant/terramino-go/main.go"
Since the backend is running with docker compose up
, rebuild the backend and restart the services to apply the change. Use the reload-terramino
provisioning script included in your Vagrantfile.
$ vagrant provision --provision-with reload-terramino
Finally, verify that the health check handler is working correctly by making a request from your host machine.
$ curl localhost:8080/health
OK
This confirms that the backend is functioning with your new update.
Next steps
In this tutorial, you configured resource sharing between the host and guest machines. You set up port forwarding, synchronized files, and verified changes to ensure seamless integration. This workflow simplifies development by connecting local code changes with services running in a virtualized environment.
Continue to the next tutorial to learn how to manage multi-machine environments with Vagrant
For more information on topics covered in this tutorial, refer to the following documentation:
- Vagrant networking, specifically forwarded ports
- Synced folders