2026 Twenty-four merry days of Perl Feed Archives

Delivering packages for Christmas with Container::Builder

Container::Builder - 2026-12-05

Delivering hundreds of millions of packages for December 25th requires a global operation. To support and track all the packages, Santa's Elves have created a lot of supporting software over the years. In more recent years they've made the transition to container images so the Sysadmin Elves don't have to provision VMs with all the dependencies for the software to run.

But the Sysadmin Elves have noticed that the container images kept getting bigger, and started containing a lot of software and libraries that are not being used by the application itself. So the Sysadmin Elves went to work to create a way to make smaller container packages more easily so the Software Elves can ship faster.

Creating a minimal container image

The Container::Builder package allows fine-grained control over the files and directories that end up in container images. Think of it as a Dockerfile in Perl code but one where you start from scratch.

Since it's easier to explain in practice, we'll make a minimal container for running a Dancer2 app. We can generate a Dancer2 project as follows:

dancer2 gen --application PerlAdvent
cd PerlAdvent

Creating a Container::Builder object

We can then start with creating the build.pl file that will make the container image. Substitute your local mirror from the list of Debian mirrors for debian_pkg_hostname. os_version picks the Debian release the packages come from (the default is bookworm). Because we'll bundle our own Perl with pp later, pick a release whose glibc is at least as new as the one your Perl was built against.

use v5.40;
use Container::Builder;
my $builder = Container::Builder->new(
    debian_pkg_hostname => 'mirror.as35701.net',
    os_version => 'trixie',
);

Adding groups and users

Let's add the base groups for a standard unix system:

$builder->add_group('root', 0);
$builder->add_group('tty', 5);
$builder->add_group('staff', 50);
$builder->add_group('larry', 1337);
$builder->add_group('nobody', 65000);

And let's make 3 users. We'll run our application under Larry.

$builder->add_user('root', 0, 0, '/sbin/nologin', '/root');
$builder->add_user('nobody', 65000, 65000, '/sbin/nologin', '/nohome');
$builder->add_user('larry', 1337, 1337, '/sbin/nologin', '/home/larry');

Adding directories

Let's also create a base directory structure:

$builder->create_directory('/', 0755, 0, 0);
$builder->create_directory('bin/', 0755, 0, 0);
$builder->create_directory('tmp/', 01777, 0, 0);
$builder->create_directory('root/', 0700, 0, 0);
$builder->create_directory('home/', 0755, 0, 0);
$builder->create_directory('home/larry/', 0700, 1337, 1337);
$builder->create_directory('etc/', 0755, 0, 0);
$builder->create_directory('app/', 0755, 1337, 1337);
$builder->create_directory('app/bin/', 0755, 1337, 1337);

Adding pre-made packages

Container::Builder also allows you to add pre-made packages from Debian mirrors. Since the Debian package format contains a TAR file with all the files and directories to be installed, we can take the Debian TAR and add it to our build. This makes it easier to add basic OS files to our container.

$builder->add_deb_package('base-files');
$builder->add_deb_package('netbase');
$builder->add_deb_package('tzdata');
$builder->add_deb_package('media-types');
my $nsswitch = <<'NSS';
# /etc/nsswitch.conf
passwd: compat
group: compat
shadow: compat
gshadow: files
hosts: files dns
networks: files
protocols: db files
services: db files
ethers: db files
rpc: db files
netgroup: nis
NSS
$builder->add_file_from_string($nsswitch, '/etc/nsswitch.conf', 0644, 0, 0);
# C dependencies (to run a compiled executable)
$builder->add_deb_package('libc-bin');
$builder->add_deb_package('libc6');
# Perl dependencies (to run a basic Perl program)
$builder->add_deb_package('libcrypt1');

In the code above we also show how to add a file from a Perl string using add_file_from_string.

Getting our application into the container

So far we've added all that's necessary to run compiled binaries that use libc. But how do we get our Perl application into the container without painstakingly adding every Perl module that our app needs? The answer lies in pre-assembling our application before adding it to our container with pp and/or App::FatPacker. In the case of pp, this module can add a Perl runtime into the packed executable so we don't have to add Perl Debian packages to our image. In the case of App::FatPacker, we're limited to only packing pure Perl modules.

Fatpacking a Dancer2 application

We use the commands that are documented in App::FatPacker:

fatpack trace --use=Template::Tiny bin/app.psgi
fatpack packlists-for `cat fatpacker.trace` > packlists
fatpack tree `cat packlists`
# Remove MIME::Types because it has a types.db
# which cannot be put inside the fatpack.
rm -rf fatlib/MIME
fatpack file bin/app.psgi > bin/fatpacked.app.psgi

The output will show that it's unable to pack several shared objects into the fatpack because binary files can't be included in a pure Perl fatpacked file. We'll deal with these dependencies below.

Packaging up the webserver

To run our Dancer2 app, we'll need a psgi webserver. We'll use pp to package plackup and also mention a list of extra modules to pack that Dancer2 needs but couldn't pack completely due to compiled code.

# On my 5.40.2 installation
pp -o fatplackup -B \
    -M Cpanel::JSON::XS -M HTML::Parser -M Ref::Util \
    -M Ref::Util::XS -M WWW::Form::UrlEncoded -M Clone \
    -M List::MoreUtils -M Template::Stash::** -M MIME::Types::* \
    -M Plack::** -M FindBin \
    /home/adri/.plenv/versions/5.40.2/bin/plackup
# On my 5.44.0 installation
pp -o fatplackup -B \
    -M Cpanel::JSON::XS -M HTML::Parser -M Ref::Util \
    -M Ref::Util::XS -M WWW::Form::UrlEncoded -M Clone \
    -M Template::Stash::** -M MIME::Types::* -M Plack::** \
    -M Sub::Util -M Crypt::URandom -M FindBin \
    /home/adri/.plenv/versions/5.44.0/bin/plackup

pp can pack the perl interpreter and core modules additionally to the modules referenced in plackup, and the modules mentioned explicitly on the command line. This allows us to make a packed file that includes all the modules as we have installed in our development environment, rather than using the Perl Debian packages that can contain versions incompatible with our application.

Preliminary testing

To make sure we're adding something that works to our container, let's test run our packed files:

$ ./fatplackup bin/fatpacked.app.psgi
Unknown configuration key 'strict_utf8'
Set strict_config => 0 to silence these warnings.
HTTP::Server::PSGI: Accepting connections at http://0:5000/

If you get an error when executing fatplackup related to missing modules in @INC, you'll need to adapt the pp commandline to include those modules.

Setting our runtime environment

Within our container image, we need to specify the runtime environment:

$builder->runas_user('larry');
$builder->set_env('PATH',
    '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin');
$builder->set_work_dir('/app');
$builder->set_entry('/app/plackup');

And finally, we'll add our Dancer2 files:

$builder->add_file('fatplackup', '/app/plackup', 0755, 1337, 1337);
$builder->add_file('bin/fatpacked.app.psgi', '/app/app.psgi',
    0755, 1337, 1337);
# Dancer2 folders
$builder->copy('views/', '/app/views', 0755, 1337, 1337);
$builder->copy('public/', '/app/public', 0755, 1337, 1337);
$builder->copy('environments/', '/app/environments', 0755, 1337, 1337);
# Config file
$builder->add_file('config.yml', '/app/config.yml', 0644, 1337, 1337);

Time to build!

Now that we've added everything to our Builder object, we can call build() to make the container image.

$builder->build('perladvent.tar');
say "Now run: podman load -i perladvent.tar";
say "Then run: podman tag "
    . substr($builder->get_digest(), 0, 12)
    . " localhost/perladvent:latest";

Let's see what we've created:

$ perl build.pl
Now run: podman load -i perladvent.tar
Then run: podman tag 549e29309493 localhost/perladvent:latest
$ ls -alh perladvent.tar
-rw-r--r-- 1 adri adri 19M Oct 21 20:50 perladvent.tar

We've managed to make our container, containing everything we need, just under 20MB.

Time to run!

Our script output gives us the commands to import the image into our (local) podman:

$ podman load -i perladvent.tar
$ podman tag 549e29309493 localhost/perladvent:latest
$ podman image list
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/perladvent latest 549e29309493 12 minutes ago 31.9 MB

When our image was loaded into podman, podman decompressed the gzipped image layers of Container::Builder, resulting in a bigger size in its output.

We can now run our container and expose the Dancer2 default port to our localhost interface as such:

$ podman run -p5000:5000 localhost/perladvent:latest /app/app.psgi

We mention /app/app.psgi as a commandline argument because we've set our entrypoint to /app/plackup. This allows the flexibility of adding any flags or options at runtime.

Where to go from here

You're now ready to start making your own minimal containers in pure Perl. If the entire process seems a bit complex for each of your projects, you can extend the Container::Builder class with your own base container steps and use that as your opinionated base image generator.

Gravatar Image This article contributed by: Adriaan Dens <ADRI@cpan.org>