How to export a Vagrant virtual machine to transfer it

前端 未结 7 1095
臣服心动
臣服心动 2021-01-29 17:29

I have a vagrant box up and running (configured with a LAMP stack). I need to transfer it to another PC. How can I export it? I guess that I can get a file (or files) that can b

相关标签:
7条回答
  • 2021-01-29 18:02

    The easiest way would be to package the Vagrant box and then copy (e.g. scp or rsync) it over to the other PC, add it and vagrant up ;-)

    For detailed steps, check this out => Is there any way to clone a vagrant box that is already installed

    0 讨论(0)
  • 2021-01-29 18:08

    This is actually pretty simple

    1. Install virtual box and vagrant on the remote machine
    2. Wrap up your vagrant machine

      vagrant package --base [machine name as it shows in virtual box] --output /Users/myuser/Documents/Workspace/my.box

    3. copy the box to your remote

    4. init the box on your remote machine by running

      vagrant init [machine name as it shows in virtual box] /Users/myuser/Documents/Workspace/my.box

    5. Run vagrant up

    0 讨论(0)
  • 2021-01-29 18:12

    None of the above answers worked for me. I have been 2 days working out the way to migrate a Vagrant + VirtualBox Machine from a computer to another... It's possible!

    First, you need to understand that the virtual machine is separated from your sync / shared folder. So when you pack your machine you're packing it without your files, but with the databases.

    What you need to do:

    1- Open the CMD of your computer 1 host machine (Command line. Open it as Adminitrator with the right button -> "Run as administrator") and go to your vagrant installed files. On my case: C:/VVV You will see your Vagrantfile an also these folders:

    /config/
    /database/
    /log/
    /provision/
    /www/
    Vagrantfile
    ...
    

    The /www/ folder is where I have my Sync Folder with my development domains. You may have your sync folder in other place, just be sure to understand what you are doing. Also /config and /database are sync folders.

    2- run this command: vagrant package --vagrantfile Vagrantfile

    (This command does a package of your virtual machine using you Vagrantfile configuration.)

    Here's what you can read on the Vagrant documentation about the command:

    A common misconception is that the --vagrantfile option will package a Vagrantfile that is used when vagrant init is used with this box. This is not the case. Instead, a Vagrantfile is loaded and read as part of the Vagrant load process when the box is used. For more information, read about the Vagrantfile load order.

    https://www.vagrantup.com/docs/cli/package.html

    When finnished, you will have a package.box file.

    3- Copy all these files (/config, /database, Vagrantfile, package.box, etc.) and paste them on your Computer 2 just where you want to install your virtual machine (on my case D:/VVV).

    Now you have a copy of everything you need on your computer 2 host.

    4- run this: vagrant box add package.box --name VVV

    (The --name is used to name your virtual machine. On my case it's named VVV) (You can use --force if you already have a virtual machine with this name and want to overwrite it. (Use carefully !))

    This will unpack your new vagrant Virtual machine.

    5- When finnished, run: vagrant up

    The machine will install and you should see it on the "Oracle virtual machine box manager". If you cannot see the virtual machine, try running the Oracle VM box as administrator (right click -> Run as administrator)

    You now may have everything ok but remember to see if your hosts are as you expected:

    c:/windows/system32/hosts
    

    6- Maybe it's a good idea to copy your host file from your Computer 1 to your Computer 2. Or copy the lines you need. In my case these are the hosts I need:

    192.168.50.4 test.dev
    192.168.50.4 vvv.dev
    ...
    

    Where the 192.168.50.4 is the IP of my Virtual machine and test.dev and vvv.dev are developing hosts.

    I hope this can help you :) I'll be happy if you feedback your go.

    Some particularities of my case that you may find:

    When I ran vagrant up, there was a problem with mysql, it wasn't working. I had to run on the Virtual server (right click on the oracle virtual machine -> Show console): apt-get install mysql-server

    After this, I ran again vagrant up and everything was working but without data on the databases. So I did a mysqldump all-tables from the Computer 1 and upload them to Computer 2.

    OTHER NOTES: My virtual machine is not exactly on Computer 1 and Computer 2. For example, I made some time ago internal configuration to use NFS (to speed up the server sync folders) and I needed to run again this command on the Computer 2 host: vagrant plugin install vagrant-winnfsd

    0 讨论(0)
  • 2021-01-29 18:12

    As stated in

    How can I change where Vagrant looks for its virtual hard drive?

    the virtual-machine state is stored in a predefined VirtualBox folder. Copying the corresponding machine (folder) besides your vagrant-project to your other host should preserve your virtual machine state.

    0 讨论(0)
  • 2021-01-29 18:13

    My hard drive in my Mac was making beeping noises in the middle of a project so I decided to install a SSD. I needed to move my project from one disk to another. A few things to consider:

    • I'm vagrant w/ virtualbox on a Mac
    • I'm using git

    This is what worked for me:

    1.) Copy your ~/.vagrant.d directory to your new machine.
    2.) Copy your ~/VirtualBox\ VMs directory to your new machine. 
    3.) In VirtualBox add the machines one by one using **Machine** >> **Add**
    4.) Run `vagrant box list` to see if vagrant acknowledges your machines. 
    5.) `git clone my_project`
    6.) `vagrant up`
    

    I had a few problems with VB Guest additions.

    enter image description here

    I fixed them with this solution.

    0 讨论(0)
  • 2021-01-29 18:14

    You have two ways to do this, I'll call it dirty way and clean way:

    1. The dirty way

    Create a box from your current virtual environment, using vagrant package command:

    http://docs.vagrantup.com/v2/cli/package.html

    Then copy the box to the other pc, add it using vagrant box add and run it using vagrant up as usual.

    Keep in mind that files in your working directory (the one with the Vagrantfile) are shared when the virtual machine boots, so you need to copy it to the other pc as well.

    2. The clean way

    Theoretically it should never be necessary to do export/import with Vagrant. If you have the foresight to use provisioning for configuring the virtual environment (chef, puppet, ansible), and a version control system like git for your working directory, copying an environment would be at this point simple as running:

    git clone <your_repo>
    vagrant up
    
    0 讨论(0)
提交回复
热议问题