Nspawn – Create Containers From Docker Images
You’ve already seen that Systemd has an entire container engine under the hood.
You may created a fully functional Debian container in it.
But what can you do when you need a different distribution?
You can convert a Docker image to an Nspawn container.
Objective
- Use
podmanas the container engine to manage the Docker images. - Create suitable containers for Nspawn.
- Use Btrfs for Nspawn templates.
In the example I will use a Rocky Linux image.
You can choose any other Linux image from your trusted Docker source.
Rocky Linux Image Example
Pull the official Rocky Linux image from the Docker Hub with podman.
podman pull docker.io/rockylinux/rockylinux:10
Start a container from the downloaded image.
podman run --name rocky-template -it docker.io/rockylinux/rockylinux:10 /bin/bash
Install the necessary tools and set up the system.
dnf update -ydnf install -y dbus NetworkManagersystemctl enable NetworkManagersystemctl enable dbusexit
Now you can export the container as a tar archive.
podman export rocky-template -o rocky-10-template.tar
Create a Btrfs subvolume for this template and move the base there.
sudo btrfs subvolume create /var/lib/machines/rocky-10-templatesudo tar -xf rocky-10-template.tar -C /var/lib/machines/rocky-10-template
Remove the machine-id and hostname from the Nspawn template.
sudo rm -f /var/lib/machines/rocky-10-template/etc/machine-idsudo rm -f /var/lib/machines/rocky-10-template/etc/hostname
Make the template image read-only:
sudo machinectl read-only rocky-10-template yes
You can clone it and run a container from it.
sudo machinectl clone rocky-10-template new-container
Final Thoughts
As you’ve seen you can convert many Docker images to functional Nspawn containers.
Images based on complete Linux distributions are particularly well suited for this approach.
This way, instead of application containers you can run OS containers easily.
The process is repeatable and easy to automate.
Discover more from Tom's IT Cafe
Subscribe to get the latest posts sent to your email.