Use Hashicorp Packer with Docker to build Bootable Image using Containers.
This repository tries its best to replicate Ivan Velichko's @iximiuz docker-to-linux repo but using a single Packer Template which can be configured for different Distribution flavors.
Result
Distribution Name | Platform | Working |
---|---|---|
Debian Bullseye Debian Bookworm |
linux/amd64 |
✔️ |
Ubuntu 20.04 (Focal Fossa) Ubuntu 22.04 (Jammy Jellyfish) |
linux/amd64 |
✔️ |
Alpine 3.16 | linux/amd64 |
✔️ |
Requirements:
- GNU Make (>=4.3)
- Docker (>=20.10.17)
- Hashicorp Packer (>=1.8.3)
Initially download the Docker plugin for Hashicorp Packer using:
make init
make ubuntu
will build an ubuntu based bootable image
make debian
will build a debian based bootable image
make alpine
will build an alpine based bootable image
make clean
removes all generated artifacts
-
Bring up the respective container, install a compatible kernel and
systemd
in it (for Debian, Ubuntu) andopenrc
for Alpine. -
Let Packer export the container's filesystem from Step 1 to a tarball. The tarball will be made available in the root directory of this repository as
{distribution}.tar
e.g.ubuntu.tar
,debian.tar
,alpine.tar
-
Unpack the filesystem tarball in a directory namely
{distribution}.dir
. This is mainly because Packer + Docker will often fail when trying to extract the tarball within the container. It will be easier to just copy the extracted content to the respective mount point in the container -
the bash script in
scripts
directory takes the responsibility for creating a bootable image which will be available in the root directory of the repo as{distribution}.img
and{distribution}.qcow2
For amd64
images, the final .img
file will be created with root
ownership, which will require sudo
privileges
sudo qemu-system-x86_64 -m 4096 -smp 4 -drive file={distribution}.img,index=0,media=disk,format=raw
here {distribution}
will be either ubuntu
, debian
, alpine
.