本文目录导读:
在当今的软件开发环境中,虚拟化技术已经成为了一个不可或缺的工具,它可以帮助我们更有效地管理硬件资源,提高开发和部署的效率,本文将详细介绍Vagrant这个流行的虚拟化工具,并带领大家从入门到精通。
什么是Vagrant?
Vagrant是一个用于构建和管理虚拟机环境的工具,它使用SSH协议来控制和管理虚拟机的创建、配置和销毁,通过Vagrant,开发者可以在本地计算机上轻松地创建和配置多个相互隔离的开发和测试环境,从而提高开发效率。
安装Vagrant
要开始使用Vagrant,首先需要在你的计算机上安装它,根据你的操作系统,可以参考Vagrant官方文档(https://docs.Vagrantup.com/installation/)进行相应的安装。
创建一个新的Vagrant项目
在安装好Vagrant之后,我们需要创建一个新的项目来存放我们的Vagrant配置文件,可以使用以下命令创建一个名为“my-app”的新项目:
mkdir my-app cd my-app
我们需要初始化一个新的Vagrant环境,在这个例子中,我们将使用一个基于Ubuntu的虚拟机作为我们的开发环境,运行以下命令:
vagrant init --box=ubuntu/bionic64 .
这将会下载一个名为“ubuntu/bionic64”的预定义虚拟机模板,并将其保存为名为“default”的Vagrant配置文件,它还会在当前目录下生成一个名为“Vagrantfile”的配置文件,用于描述我们的虚拟机环境。
配置Vagrantfile
打开“Vagrantfile”文件,可以看到以下内容:
-*- mode: ruby -*- vi: set ft=ruby : Be more verbose, but avoid verbose output to not pollute the CI logs. See https://github.com/vagrantup/vagrant/issues/5800 for details. set :verbose_flag false Print Vagrant's version at every command. We only print the version if it has changed. This makes our CI logs cleaner. See https://github.com/vagrantup/vagrant/issues/3721 for details. set :show_version_in_status true Tell Vagrant that we're using a box that requires an extra layer to be installed on top of it. In this case, that layer is Ubuntu Bionic. See https://github.com/mitchellh/vagrant-ubuntu-bionic/tree/master for more information. vm_box = "ubuntu/bionic64" Set the default box to be used when creating new virtual machines. This will be in your Vagrantfile, which is usually located in the root directory of your project. See all available boxes at https://hub.docker.com/_/ubuntu?tab=stars&page=1. We are using the latest version of Ubuntu Bionic here. See https://hub.docker.com/r/ubuntu/bionic for more information. vm_provider = "virtualbox"
根据你的需求,可以根据官方文档(https://docs.vagrantup.com/v2/configuring-a-custom-vm)对这些设置进行修改,如果你想使用其他类型的虚拟机或者自定义镜像,可以修改vm_box
和vm_provider
的值。
启动Vagrant虚拟机
在配置好Vagrantfile之后,我们可以通过运行以下命令来启动我们的虚拟机:
vagrant up
这将会下载所需的依赖包并启动虚拟机,如果一切正常,你应该会看到类似以下的输出:
[192.168.33.10] Vagrant reloaded! Running vagrant with hooks provisioning complete in 0.15 seconds (using 100% of memory) ... [192.168.33.10] guest@ubuntu:~$ ls -alhR $(pwd) # For debugging purposes only! This shows the current working directory and its permissions on the guest OS! Please do not use this in production environments! The Vagrantfile should contain more robust debugging tools! If you need to debug Vagrant from the host OS, please consult the docs at https://docs.vagrantup.com/v2/synced-folders/#debugging-guest-os-commands! You can also runvagrant ssh
to log in to the guest OS and run commands there! See https://docs.vagrantup.com/v2/synced-folders/ for more information about synced folders! If you have any issues with synced folders or other guest OS functionality, please consult the docs at https://docs.vagrantup.com/v2/synced-folders/ and https://docs.vagrantup.com/v2/synced-folders/#debugging-guest-os-commands! You can also runvagrant status
to see the status of your VM and troubleshoot any issues that may arise! If you have any questions or concerns about Vagrant, please consult the documentation at https://docs.vagrantup.com/ or ask on Stack Overflow or GitHub! Thank you for using Vagrant! Happy hacking! #{timestamp} [192.168.33.10] guest@ubuntu:~$ exit # Exiting the shell session on the guest OS! If you need to keep running commands after exiting this shell session, please add them to a script and run that script instead! For example, you could add the following lines to a script namedstartup
and run that script instead of runningexit
: echo "Running startup script" && sleep 5 && echo "Running shutdown script" && sudo shutdown -h now # To run a script after this shell session ends without restarting the VM, you can runvagrant halt
and then start the VM again withvagrant up
. # To remove this shell session and stop running any additional commands on the guest OS, you can simply typeexit
at this prompt and Vagrant will automatically shut down the VM and terminate any remaining processes on the guest OS! #{timestamp} [192.168.33.10] guest@ubuntu:~$ exit # Bye! Have a great day! :-) #{timestamp} [192.168.33.10] guest@ubuntu:~$ exit #{timestamp} [192.168.33.10] guest@ubuntu:~$ exit #{timestamp} [192.168.33.10] guest@ubuntu:~$ exit #{timestamp} [192