Packer
Contents TL;DR | Overview | Getting started | Usage | Next steps | Contributing | Resources
This repository contains Packer samples for Docker, IIS, SQL Server and Visual Studio on Windows and Ubuntu, building virtual machine images and Vagrant boxes for VirtualBox, VMware and Hyper-V, provisioned with Chef.
TL;DR
- Vagrant boxes ready to use for virtualizing hosting and development scenarios.
- Virtual workstations for automating the configuration of your development environments.
- Blogs with an overview of the why, how and what of Packer.
Overview
Contents Operating systems | Hosting | Development
Note This section covers the details of the published Vagrant boxes this repository builds. See the Getting started section to build your own virtual machine images. See virtual workstations for samples of automating the configuration of your development environments using them and these blogs for more background and motivation.
This repository contains Packer sample templates for the following virtualization scenarios:
- Operating systems for generic experiments with Windows and Ubuntu.
- Hosting using Docker, IIS and SQL Server.
- Development using Visual Studio.
The virtual machine images and Vagrant boxes are built for VirtualBox, VMware and Hyper-V, and are provisioned using Chef.
Note All the components, including the core operating systems, share the following characteristics:
- They are based on their publicly available versions. You might need to provide your own license(s) (for example, a valid Windows or Visual Studio license) to start or keep using them after their evaluation periods expire.
- They are installed using their latest available versions. The latest patches (for example, all the Windows Updates) are applied as well.
- Unless noted otherwise, they are installed using the default configuration options.
Operating systems
The following Vagrant boxes can be used for generic experiments on the respective platforms. They contain the core operating system with the minimum configuration required to make Vagrant work, and some of the commonly used tools installed and options configured for easier provisioning. All the other Vagrant boxes below are based on these configurations as well.
Windows
Windows Server
- Windows Server
- Windows Server 2022 Standard
- Windows Server 2022 Standard No Configuration Management
- Windows Server 2019 Standard
- Windows Server Insider Preview Standard
Windows Server Core
- Windows Server Core
- Windows Server 2022 Standard Core
- Windows Server 2019 Standard Core
- Windows Server Insider Preview Standard Core
Windows 11
- Windows 11
- Windows 11 Version 22H2 Enterprise
- Windows 11 Version 21H2 Enterprise
- Windows 11 Insider Preview Enterprise
Windows 10
- Windows 10
- Windows 10 Version 22H2 Enterprise
- Windows 10 Version 22H2 Enterprise No Configuration Management
- Windows 10 Version 21H2 Enterprise
- Windows 10 Version 21H2 Enterprise LTSC
- Windows 10 Version 1809 Enterprise LTSC
- Windows 10 Insider Preview Enterprise
Ubuntu
Ubuntu Server
Ubuntu Desktop
Hosting
The following Vagrant boxes can be used for hosting scenarios. They contain the respective hosting tools with the default configuration are based on the core operating systems.
Docker
- Docker Windows
- Docker Community on Windows Server
- Docker Community on Windows Server Core
- Docker Linux
- Docker Community on Ubuntu Server
IIS
SQL Server
- SQL Server
- SQL Server 2019 Developer on Windows Server
- SQL Server 2019 Developer on Windows Server Core
Development
The following Vagrant boxes can be used for development scenarios including setting up virtual workstations. They contain the respective development tools with the common configuration and are based on the core operating systems.
Visual Studio
- Visual Studio
- Visual Studio 2022 Community on Windows 11
- Visual Studio 2022 Community on Windows 10
- Visual Studio 2019 Community on Windows 11
- Visual Studio 2019 Community on Windows 10
Getting started
Note The rest of this document covers the details of building virtual machine images and Vagrant boxes, and assumes that you are familiar with the basics of Packer and Vagrant. If that's not the case, it's recommended that you take a quick look at the getting started guides.
Note Building the Packer templates have been tested on Windows hosts only, but they are supposed to run on any other platform as well, given that the actual virtualization provider (e.g. VirtualBox) supports it. Let me know if you encounter any issues and I'm glad to help.
Follow the steps below to install the required tools:
- Install the .NET Core SDK with Cake Build.
- Install Packer and Vagrant.
- Install the tools for the virtualization provider you want to use.
- VirtualBox Install VirtualBox.
- VMware Install VMware.
- Hyper-V Enable Hyper-V.
- Install Chef Workstation.
You are now ready to build a virtual machine image and a Vagrant box.
Note It is recommended to set up caching for Packer, so you can reuse the downloaded resources (e.g. OS ISOs) across different builds. Make sure you have a bunch of free disk space for the cache and the build artifacts.
Usage
Contents Building base images | Building images for distribution | Chaining builds further | Testing | Cleaning up
This repository uses some custom wrapper scripts using Cake to generate the Packer templates and the related resources (e.g. the unattended install configuration) required to build the virtual machine images. Besides supporting easier automation, this approach helps with reusing parts of the templates and the related resources, and makes chaining builds and creating new configurations quite easy.
Building base images
Clone this repo including the submodules, and navigate to the root directory of the clone in your shell. Type the following command to list all the available templates you can build:
$ dotnet tool restore
$ dotnet cake [--target=info]
The output will be contain the section info
with the list of the templates:
...
========================================
info
========================================
...
ws2022s-virtualbox-core: Info
ws2022s-virtualbox-vagrant: Info
ws2022s-vmware-core: Info
ws2022s-vmware-vagrant: Info
ws2022s-hyperv-core: Info
ws2022s-hyperv-vagrant: Info
...
w1022h2e-virtualbox-core: Info
w1022h2e-virtualbox-vagrant: Info
w1022h2e-vmware-core: Info
w1022h2e-vmware-vagrant: Info
w1022h2e-hyperv-core: Info
w1022h2e-hyperv-vagrant: Info
...
ws2022s-iis-virtualbox-core: Info
ws2022s-iis-virtualbox-vagrant: Info
ws2022s-iis-vmware-core: Info
ws2022s-iis-vmware-vagrant: Info
ws2022s-iis-hyperv-core: Info
ws2022s-iis-hyperv-vagrant: Info
...
You can filter this further to list only the templates for a given virtual machine image type. For example, to list the templates based on the Windows Server 2022 Standard
image, invoke the info
command with the ws2022s
argument:
$ dotnet cake [--target=info] --configuration=ws2022s
You can use this filtering with all the dotnet cake
commands below as well. It selects all the templates which contain the specified argument as a substring, so you can filter for components (ws2022s
, w1022h2e
, iis
, etc.) or providers (virtualbox
, vmware
, hyperv
) easily.
The output will contain only the matching templates:
...
========================================
info
========================================
ws2022s-virtualbox-core: Info
ws2022s-virtualbox-vagrant: Info
ws2022s-vmware-core: Info
ws2022s-vmware-vagrant: Info
ws2022s-hyperv-core: Info
ws2022s-hyperv-vagrant: Info
...
This means that this configuration supports building native base images (virtualbox-core
, vmware-core
, hyperv-core
) mainly for reusing them in other configurations, and also, Vagrant boxes for distribution (virtualbox-vagrant
, vmware-vagrant
, hyperv-vagrant
). Under the hood, the vagrant
configurations will simply start from the output of the core
ones, so build times can be reduced significantly.
Now, invoke the restore
command with the name of the template you want to build to create the resources required by Packer. For example, for VirtualBox, type the following command:
$ dotnet cake --target=restore --configuration=ws2022s-virtualbox-core
This will create the folder build/ws2022s/virtualbox-core
in the root of your clone with all the files required to invoke the Packer build. This setup is self-contained, so you can adjust the parameters manually in template.json
or the other resources and / or even copy it to a different machine and simply invoke packer build template.json
there. Most of the time though, you just simply want to build as it is, as the templates are already preconfigured with some reasonable defaults. This can be done of course with the build script as well:
$ dotnet cake --target=build --configuration=ws2022s-virtualbox-core
This will trigger the Packer build process, which usually requires only patience. Depending on the selected configuration, a few minutes or hours later, the build output will be created, in this case in the build/ws2022s/virtualbox-core/output
directory in the root of your clone. Virtual machine images like this can be directly used with the respective virtualization provider on the host machine.
Building images for distribution
As mentioned above, based on Packer's support for starting builds from some virtualization providers' native image format, builds can reuse the output of a previous build. To build and image which can be distributed (e.g. after applying Sysprep as well), type the following command:
$ dotnet cake --target=build --configuration=ws2022s-virtualbox-vagrant
Note that this will include restoring the build folder with the template and the related resources automatically, and then invoking the build process in a single step. It will also reuse the output of the ws2022s-virtualbox-core
build, so it does not need to do the same steps for a Vagrant box the original build already included (e.g. the core OS installation itself, installing Windows updates, etc.). Once the build completes, the Vagrant box will be available in the build/ws2022s/virtualbox-vagrant/output
folder.
The same approach works for VMware and Hyper-V as well:
$ dotnet cake --target=build --configuration=ws2022s-hyperv-core
$ dotnet cake --target=build --configuration=ws2022s-hyperv-vagrant
As you can expect, for these samples the build artifacts will be created in the builds/ws2022s
folder as well, this time under the hyperv-vagrant/output
subfolder. You can use the standard options to distribute them to be consumed in Vagrant.
Chaining builds further
Similarly to the process above, you can use build chaining to build more complex boxes. For example, the configuration for Windows Server 2022 Standard
with IIS
can be built like this:
$ dotnet cake --target=build --configuration=ws2022s-virtualbox-core
$ dotnet cake --target=build --configuration=ws2022s-iis-virtualbox-core
$ dotnet cake --target=build --configuration=ws2022s-iis-virtualbox-vagrant
As in the previous ws2022s
sample, for this configuration the ws2022s-iis-virtualbox-core
build will start from the output of ws2022s-virtualbox-core
instead of starting with the core OS installation. Chanining builds like this has no limitations, so you can use this approach to build images with any number of components very effectively.
Note that the script can invoke the build of the dependencies automatically, so for the previous example you can simply type:
$ dotnet cake --target=build --configuration=ws2022s-iis-virtualbox-vagrant --recursive true
This will in turn invoke the restore
and build
stages for the ws2022s-virtualbox-core
and ws2022s-iis-virtualbox-core
images as well. By default, restore
and build
is skipped if the output from a previous build exists. You can force the build to run again using the rebuild
command instead, which will clean
the build directories first.
Again, this works for VMware and Hyper-V as well:
$ dotnet cake --target=build --configuration=ws2022s-iis-hyperv-vagrant --recursive true
Similarly, this will in turn build the ws2022s-hyperv-core
and ws2022s-iis-hyperv-core
images first if they are missing.
Testing
To help testing the build results, the reposiory contains a simple Vagrantfile to create virtual machines using directly the build outputs. You can play around with the Vagrant CLI yourself, or let the build script manage it for you.
For example, to test the ws2022s
configuration, from the root of your clone you can type the following command to use the box files in the build\ws2022s
folder:
$ dotnet cake --target=test --configuration=ws2022s-virtualbox-vagrant
This will import the locally built Vagrant box temporarily with the name ws2022s-build
and will use that to spin up a new virtual machine. It also outputs some basic diagnostics information to help quickly checking the installations.
Cleaning up
Though the build
folders are excluded by default from the repository, they can consume significant disk space. You can manually delete the folders, but the build script provides support for this as well:
$ dotnet cake --target=clean --configuration=ws2022s-iis-virtualbox-vagrant
Using the filtering, to clean up the artifacts of all the VirtualBox builds, you can type:
$ dotnet cake --target=clean --configuration=virtualbox
Omitting this parameter will apply the command to all the templates, so the following command will clean up everything:
$ dotnet cake --target=clean
Note The
clean
command cleans up dependencies recursively, including the eventually imported Vagrant boxes and virtual machines created using thetest
command.
Next steps
Take a look at the repository of virtual workstations to easily automate and share your development environment configurations using the Vagrant boxes above.
Contributing
Feedback, issues or pull requests are welcome and are greatly appreciated. Check out the milestones for the list of planned releases.
Resources
This repository could not exist without the following great technologies:
This repository borrows awesome ideas and solutions from the following sources: