I was wondering if it is possible to create a Jail with all the packages we need and just use it as a template for new deployments.
I don't see why not. See, keep well in mind that a Jail isn't some magical virtual environment, it's merely a new installation of the base system of which you made your own system aware so that it would spawn a new kernel thread to accommodate for that new system.
Even the FreeBSD base system itself isn't something special where installation is concerned. Did you know that installing a FreeBSD base system basically boils down to unpacking
base.txz and
kernel.txz?
For example, this is the heart of my Jail environments:
Code:
peter@zefiris:/opt/jails $ ls
base.txz kernel.txz lib32.txz psi/
... well, this and a few shell scripts which perform the actual process of unpacking and applying the necessary changes to certain system files such as
/etc/rc.conf of course. That's all there is to it.
So... if you install ports what happens with those? They get installed to
/usr/local. So it should be relatively easy to simply archive that stuff and then use that as template.
Unless of course you're doing the right thing and you keep your system up to date and also upgrade the ports when there's a need. In that case my recommendation would be to build your own package repository. Look into
pkg-repo(8) to see how to start doing that.
It's really very easy... Basically all you need is a huge collection of packages. You can easily collect those yourself; either by checking your cache if you're already using
pkg install
(see
/var/db/pkg, this is the original location) or by creating packages yourself, for example from your currently installed packages. You can do that using the
pkg-create(8) command. Once you have a huge collection you'll need to create an index so that remote clients can quickly look up the stuff they need. That is where
pkg-repo comes into play.
For example... If you wanted to create a repository from your currently installed packages?
Code:
# pkg create -ao /var/db/myrepo/All
# pkg repo /var/db/myrepo
Once you have this setup all you have to do next is to make sure that your new jail is aware of this new repository (create a config file based on
/etc/pkg/FreeBSD.conf and dump that into
/usr/local/etc/pkg/repos). After that there's only 1 thing left to do: running
pkg install
to quickly install the required Ports.
Depending on how your application is set up you could even use this methodology to distribute that application itself as well.
pkg-create is also perfectly capable of creating a package based on an existing directory structure.
The problem is that I'm not sure if the jail would work correctly after being migrated to different machines, even though they have the same FreeBSD version installed.
I don't see why it wouldn't. A Jail isn't some kind of virtual machine running 'on top' of your OS. It's basically a new kernel thread which has been instructed to spawn a new instance based on the userland found in your Jail root. And a little bit more of course but what I'm getting at is that a Jail isn't comparable to, say, VirtualBox.