Packaging a base box with a custom Vagrantfile

2019-03-17 20:45发布

问题:

I have a specific Vagrantfile for a lucid32 box that installs some pretty standard packages (PHP/MySQL/Apache), does some specific port forwarding and so on, I set it up, checkout a project from git and change some server configurations, I want to package this box for other developers on the team to use, so I use the command:

vagrant package boxname --output test.box --vagrantfile Vagrantfile

I get a test.box file, it notes in the packaging that it's adding my specific Vagrantfile, then I run:

vagrant box add boxname test.box

It appears in vagrant box list just fine, now when I create some test directory, and do vagrant init boxname followed by vagrant up and then it provisions that box with the files from the checkout and everything, but does not setup the proper port forwarding, and is in fact not using my Vagrantfile I packaged it with at all, but generated a new default one in that directory.

I noticed in ~/.vagrant.d/boxes/boxname it has the default Vagrantfile, along with the one I packaged in includes/_Vagrantfile

Is there any way I can get the specific Vagrantfile to be the one generated when I do vagrant init boxname?

回答1:

This is actually all working as intended. Vagrant loads multiple Vagrantfiles (not just the one from vagrant init) when loading up, and the Vagrantfile packaged with a box is one of those. For more information, read about the "Vagrantfile Load Order" here: https://www.vagrantup.com/docs/vagrantfile/#load-order-and-merging

In a nutshell: Vagrant loads multiple Vagrantfiles in a specific order, and merges their configurations. The box Vagrantfile is loaded as part of this process prior to the root Vagrantfile (the one from vagrant init).

There is no way currently to make a skeleton Vagrantfile that is generated with vagrant init.

The configuration you put in the packaged Vagrantfile, such as port forwards, should be loading in properly, since when merging configurations, it should just append all the port forwards.

If this isn't happening, you should report a bug! But for the sake of a SO answer, this is how things are supposed to behave.



回答2:

This code worked for me

vagrant package --output vagrant_example.box