Home > Back-end >  System reboot and shut-down from flask app inside docker
System reboot and shut-down from flask app inside docker

Time:10-31

I have an application that is now working on Ubuntu, but eventually it should work on Raspberry Pi. Whole app is turn on with docker-compose up. There are three containers inside it, one of it is Flask Api written in Python. Now I need to add two endpoints for my Flask Api. One for system shutdown and one for system reebot.

I wanted to write some python code or bash script that would perform this actions, but the problem here is that everyting is working on docker. So for example simple linux command shutdown -h now is not working, because docker is telling that shutdown is not found. Is it possible to shutdown ubuntu/ raspberry pi from app running in docker?

CodePudding user response:

On Debian based systems (assuming you are running raspbian on your raspberry pi) shutdown and reboot are just symlinks to systemctl.

So you need to be able to run systemctl commands on the host from within docker. This can be achieved with a few volume mounts. Here is an example snippet for the docker-compose file:

  ShutdownContainer:
  image: ShutdownImage:latest
  links:
    - "rebootContainer:latest"
  volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup
      - /bin/systemctl:/bin/systemctl 
      - /run/systemd/system:/run/systemd/system 
      - /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket 
      - /sys/fs/cgroup:/sys/fs/cgroup
  command:
    - "startYourWebserver"

With these volumes mapped you should be able to run systemctl shutdown or systemctl reboot from within the docker container. It might be necessary to run the containers in privilege mode.

  • Related