I am building a minimal Debian image with debootstrap, chroot in it and installing grub.

Now I need to install some custom self created debian packages. These packages have postinst scripts running database migrations. BUT in my chroot env the postgresql of course is not running.

I tried systemd-nspawn but without the -b flag the container is not booted and again the postgresql is not running.

Of course apt install ... fails and running apt install during the first real boot would probably rerun the postinst successfully but it feels ugly.

Is there a better way how to prepare a minimal clean bootable already configured image?


2 Answers 2


Why not start PostgreSQL within your chroot before installing your custom packages?

By default systemd services won't run within a chroot, but you can still manually start PostgreSQL, e.g. something like this:

 su postgres -c 'pg_ctl start -D /usr/local/pgsql/data -l serverlog'

(Command taken from PostgreSQL 11 docs)

If it fails to start, check the serverlog file as to why.


Running the image with systemd-nspawn --boot, waiting for it to boot and then running commands with systemd-run is working



wait_for_container() {

  while true; do

    if machinectl | grep --quiet "$MACHINE_NAME"; then

    echo "Wait for container $MACHINE_NAME"
    sleep 1


  echo "Container up!"


poweroff_container() {

  machinectl poweroff $MACHINE_NAME


systemd-nspawn --boot --machine $MACHINE_NAME --directory "$MOUNT_PATH" > /dev/null 2>&1 &

# wait until container is up

systemd-run --machine $MACHINE_NAME --pipe --wait /bin/bash <<EOF 

  # do your stuff
  hostnamectl set-hostname your-hostname



You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .