-
Notifications
You must be signed in to change notification settings - Fork 4
Packaging a Custom Box
The reasons for creating a base Box vary, but the greatest advantage is speed! The WUB installer is extremely flexible, but it's also extremely slow, given the fact that it's an installer, and not simply a VM that is already precompiled for you. By creating a custom base Box, you will reduce the time it takes to spin-up a new VM by about 97%!
$ cd ~/vms;
$ git clone [email protected]:websharks/ubuntu-bootstrap.git my.vm;
$ cd ~/vms/my.vm;
When you bring up a new VM, tell the WebSharks Ubuntu Bootstrap that you want to enter Packaging Mode. This is done from the command-line on the Host (i.e., from your own personal system) with environment variable VM_4PKG=1
.
$ cd ~/vms/my.vm;
$ VM_4PKG=1 vagrant up;
$ vagrant ssh
$ sudo /bootstrap/src/installer
A dialog will open so you can choose your installation options; e.g., installing your choice of Nginx or Apache, MySQL, firewall, etc, etc. The defaults (in 'Packaging Mode') should work just fine in most cases, but you can customize all you like.
Note: From the installation dialog, one option that you should NOT choose to install, is 'WordPress VM Symlinks'. Symlinks are disabled by default in Packaging Mode. Shared folder symlinks should not become a part of any base image.
Note: You can install any additional software you'd like after the WUB installer is finished; e.g., using $ apt-get install ...
However, please note that you should not do anything on this VM that would leave it in a state that is not suitable for easy reuse later. For instance, don't access the VM from a web browser and install a bunch of WordPress themes/plugins. You want the base Box to be just that, a base to work from later. What most people want from a base image is Nginx/Apache, MySQL, and PHP.
When you're all done with software installation, log out of the VM.
$ exit
$ cd ~/vms/my.vm
$ mkdir -p ~/vms/my-boxes;
$ vagrant package --output ~/vms/my-boxes/my.box;
🎉 Congratulations! You now have a base Box to work from. The next time you bring up a VM, instead of waiting for the WUB installer to download and install all of the base packages/libraries that it needs, you have an already-compiled base Box that you can spin-up in just a few seconds whenever you like, for as many instances as you need.
$ cd ~/vms/my.vm
$ vagrant box add [YOU]/[DESCRIPTIVE SLUG] ~/vms/my-boxes/my.box;
# e.g., vagrant box add jaswsinc/ubuntu-xenial64-wub-nginx ~/vms/my-boxes/my.box;
$ cd ~/vms/my.vm
$ vagrant destroy --force;
$ cd ~/vms;
$ rm -rf my.vm;
These instructions use an prepared Vagrantfile
template (provided by the WUB) that is designed specifically for this purpose; i.e., to be used when spinning up a new VM based on a custom Box.
$ mkdir ~/vms/my-custom.vm;
$ cd ~/vms/my-custom.vm;
$ curl https://raw.githubusercontent.com/websharks/ubuntu-bootstrap/dev/.box.Vagrantfile -Lo Vagrantfile;
$ vi Vagrantfile;
Change this line:
config.vm.box = 'websharks/ubuntu-xenial64';
To the name of your base Box:
config.vm.box = '[YOU]/[DESCRIPTIVE SLUG]';
# e.g., config.vm.box = 'jaswsinc/ubuntu-xenial64-wub-nginx';
Tip: At this point, it would be a good idea to start a new personal GitHub repository with your Vagrantfile
being the only file for now, that's fine. Just save it somewhere so you can use it again later as a faster starting point. Or you can share your repo with others.
For example, if you have hub
...
$ cd ~/vms/my-custom.vm;
$ git init
$ git add --all
$ git commit -m 'Initialzing VM repo.'
$ git create [YOU]/[DESCRIPTIVE SLUG]
# e.g., git create jaswsinc/ubuntu-xenial64-wub-nginx
$ cd ~/vms/my-custom.vm;
$ vagrant up # This should take just a few seconds.
This assumes that you didn't install WordPress in the base Box already. By default, WordPress is not installed when you build a base Box in 'Packaging Mode', and that's suggested. Instead, $ wp
(WP CLI) can do it without much fuss.
$ vagrant ssh;
$ cd /app/src;
$ wp core download;
# All of these variables already exist in any WUB installation.
$ wp core config --dbhost="${CFG_MYSQL_DB_HOST}" --dbname="${CFG_MYSQL_DB_NAME}" \
--dbcharset="${CFG_MYSQL_DB_CHARSET}" --dbcollate="${CFG_MYSQL_DB_COLLATE}" \
--dbuser="${CFG_MYSQL_DB_USERNAME}" --dbpass="${CFG_MYSQL_DB_PASSWORD}";
$ wp core install --url=https://my-custom.vm --title=WordPress \
--admin_user=admin --admin_password=admin [email protected];
Tip: Another way to install and configure WordPress (ahead of time) is to simply include it in your GitHub repo and mount it on the VM using your Vagrantfile
. The web accessible directory on the VM is /app/src
. So that's where WordPress should go. For example, in your custom Vagrantfile
...
config.vm.synced_folder './wordpress', '/app/src', mount_options: ['defaults', 'uid=www-data', 'gid=www-data', 'umask=002'];
-
Learn more about your
Vagrantfile
. -
If you'd like to share your
.box
file with others, create an account at Hashicorp and upload the~/vms/my-boxes/my.box
file, then 'release' it, giving others a fast track to the same fun you've had. See: https://www.hashicorp.com/