Rebooting raspberry pi host from within docker container

I have a python application that’s meant to be run on a raspberry pi. I’ve created a docker-compose file to set it up, and my entry point happens to be a shell script that checks various things on the host such as:

  1. Making sure SPI is enabled, if it is not then enabling it by accessing /boot/config.txt and writing to it.
  2. Installing and enabling the watchdog service.
  3. Running my docker container automatically on reboot by writing it into /etc/rc.local (although I am considering replacing this with the restart: always or unless-stopped flag from the docker-compose file)

The problem is, if I enable SPI, the raspberry pi needs to reboot to set it up (not quite sure why), but when my shell script reaches the sudo reboot command from within the docker container, I get the following error:

 Failed to connect to bus: No such file or directory
 Failed to talk to init daemon.

I understand that it’s probably trying to find dbus and the init daemon within the docker container but they don’t exist. How do I give my container access to these resources? Do I need to mount another volume? This is my docker-compose.yml file:

version: "3"

        restart: unless-stopped
            - "/data/db:/data/db"
            - "27017:27017"
            - "28017:28017"
        image: "andresvidal/rpi3-mongodb3:latest"
        restart: unless-stopped
            - "1883:1883"
        image: "mjenz/rpi-mosquitto"
        privileged: true
        network_mode: "host"
            - "mosquitto"
            - "mongoDB"
            - "/home/pi:/home/pi"
            - "/boot:/boot"
        #image: "arkfreestyle/fg:v1.8"
        image: "test:latest"
        entrypoint: /app/
        restart: unless-stopped

FG is my python application with the entry point which looks like:

if [ ! -f /home/pi/.initialized ]; then                                                                                                                                                                                    
    echo "Initializing..."                                                                                                                                                                                

    # Turn spi on
    if grep -Fxq "dtparam=spi=on
dtparam=watchdog=on" /boot/config.txt
        echo "nSPI is already enabled"
        echo "Creating .initialized"
        # Create .initialized hidden file
        touch /home/pi/.initialized
        echo "Starting application..."
        sudo python3 -debug
        ### Enable SPI ###

    ### Create .initialized file ### 
    echo "Rebooting in ten seconds..."
    sleep 10
    sudo reboot # This line results in the error

    echo "Initialized already!"
    sudo python3 -debug                                                                                                                                                                                     

The privileged option already gives my container access to the GPIO, I imagined it would give me access to reboot as well, however it seems that is not the case. Please let me know what I need to do to be able to reboot.

Source: StackOverflow