➜ ~ vagrant box list ubuntu/xenial64 (virtualbox, 20170815.1.0)
接着创建一个目录来存放Vagrantfile mkdir bboysoul cd bboysoul/ 初始化,此操作会在当前目录下生成一个Vagrantfile
1 2 3 4 5 6 7 8
➜ bboysoul vagrant init ubuntu/xenial64 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. ➜ bboysoul ls Vagrantfile ➜ bboysoul
➜ bboysoul vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'ubuntu/xenial64'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'ubuntu/xenial64' is up to date... ==> default: Setting the name of the VM: bboysoul_default_1503234312551_82817 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Running 'pre-boot' VM customizations... ==> 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: ubuntu default: SSH auth method: password default: Warning: Connection reset. Retrying... default: Warning: Remote connection disconnect. Retrying... 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: 5.0.40 default: VirtualBox Version: 5.1 ==> default: Mounting shared folders... default: /vagrant => /home/bboysoul/bboysoul
Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud
0 packages can be updated. 0 updates are security updates.
_____________________________________________________________________ WARNING! Your environment specifies an invalid locale. The unknown environment variables are: LC_CTYPE=zh_CN.UTF-8 LC_ALL= This can affect your user experience significantly, including the ability to manage packages. You may install the locales by running:
sudo apt-get install language-pack-zh or sudo locale-gen zh_CN.UTF-8
To see all available language packs, run: apt-cache search "^language-pack-[a-z][a-z]$" To disable this message for all users, run: sudo touch /var/lib/cloud/instance/locale-check.skip _____________________________________________________________________
➜ bboysoul vagrant reload ==> default: Attempting graceful shutdown of VM... ==> default: Checking if box 'ubuntu/xenial64' is up to date... ==> 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 (guest) => 2222 (host) (adapter 1) ==> default: Running 'pre-boot' VM customizations... ==> 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: ubuntu default: SSH auth method: password ==> 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: 5.0.40 default: VirtualBox Version: 5.1 ==> default: Mounting shared folders... default: /vagrant => /home/bboysoul/bboysoul ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.
你可以使用 vagrant halt 来关闭虚拟机
1 2
➜ bboysoul vagrant halt ==> default: Attempting graceful shutdown of VM...
➜ bboysoul vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Checking if box 'ubuntu/xenial64' is up to date... ==> 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 (guest) => 2222 (host) (adapter 1) ==> default: Running 'pre-boot' VM customizations... ==> 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: ubuntu default: SSH auth method: password ==> 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: 5.0.40 default: VirtualBox Version: 5.1 ==> default: Mounting shared folders... default: /vagrant => /home/bboysoul/bboysoul ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.
➜ bboysoul vagrant list-commands Below is a listing of all available Vagrant commands and a brief description of what they do.
box manages boxes: installation, removal, etc. cap checks and executes capability connect connect to a remotely shared Vagrant environment destroy stops and deletes all traces of the vagrant machine docker-exec attach to an already-running docker container docker-logs outputs the logs from the Docker container docker-run run a one-off commandin the context of a container global-status outputs status Vagrant environments for this user halt stops the vagrant machine help shows the helpfor a subcommand init initializes a new Vagrant environment by creating a Vagrantfile list-commands outputs all available Vagrant subcommands, even non-primary ones login login to HashiCorp's Vagrant Cloud package packages a running vagrant environment into a box plugin manages plugins: install, uninstall, update, etc. port displays information about guest port mappings powershell connects to machine via powershell remoting provider show provider for this environment provision provisions the vagrant machine push deploys code in this environment to a configured destination rdp connects to machine via RDP reload restarts vagrant machine, loads new Vagrantfile configuration resume resume a suspended vagrant machine rsync syncs rsync synced folders to remote machine rsync-auto syncs rsync synced folders automatically when files change share share your Vagrant environment with anyone in the world snapshot manages snapshots: saving, restoring, etc. ssh connects to machine via SSH ssh-config outputs OpenSSH valid configuration to connect to the machine status outputs status of the vagrant machine suspend suspends the machine up starts and provisions the vagrant environment validate validates the Vagrantfile version prints current and latest Vagrant version
现在列举一些常用的
vagrant box 用来管理box的
vagrant box add 添加一个box
vagrant box list 列出可用的box
vagrant box remove 删除一个box
vagrant box
vagrant halt 停止一个虚拟机
vagrant init 初始化一个虚拟机
vagrant package 打包一个虚拟机
vagrant reload 重启一个虚拟机
vagrant ssh ssh进虚拟机
vagrant up 启动一个虚拟机
vagrant status 查看虚拟机状态
打包分发虚拟机
当你把所有的环境都配置好之后,下一步要做的就是打包分发了 一个命令就可以搞定你的打包
1 2 3 4 5 6 7
➜ bboysoul vagrant package ==> default: Attempting graceful shutdown of VM... ==> default: Clearing any previously set forwarded ports... ==> default: Exporting VM... ==> default: Compressing package to: /home/bboysoul/bboysoul/package.box ➜ bboysoul ls package.box ubuntu-xenial-16.04-cloudimg-console.log Vagrantfile