The networking is actually surprisingly easy. By default (at least with the versions that I've used), the container will use the host's network interfaces. There are many ways of doing this, but the following recipe to set up a container to run a VNC client using systemd-nspawn
(on a console only host) should provide a good basis for further experimentation:-
The host will need the following packages to be installed: debootstrap
, systemd-container
, sway
, xwayland
(xwayland
is only required because tigervnc-viewer
is not a native Wayland app).
To create the container:
debootstrap --force-check-gpg --include=systemd-container,dbus,tigervnc-viewer bookworm /var/lib/machines/vncviewer/ https://deb.debian.org/debian
Then set up the container. Get a root prompt with:
systemd-nspawn -D /var/lib/machines/vncviewer/
Then add a non-root user (e.g. 'theuser'):
adduser theuser
Finally, enable basic networking in the container, and exit it:
systemctl enable systemd-networkd
exit
Some config for the container is required. Create the file /etc/systemd/nspawn/vncviewer.nspawn
with this content :
[Exec]
Environment=DISPLAY=:0
To run the container:
systemd-nspawn -D /var/lib/machines/vncviewer/ --user=theuser vncviewer <IP address of a VNC server>
To stop it, you can either kill the systemd-nspawn
process, or (IIRC) machinectl poweroff vncviewer
To completely remove it, you can just delete /var/lib/machines/vncviewer
and /etc/systemd/nspawn/vncviewer.nspawn
.