What is the most appropriate way to shutdown the host system from within docker

103 Views Asked by At

My container runs a webserver and some sensors. It is intended as a edge compute unit for field use. I've had issues in the past with the none dockerised version, when it was turned off without using the webserver shutdown button.

The recent design change has allowed me to add a button tied to a GPIO pin. My container runs in privileged mode, I'm assuming that I can monitor this button and run a script with "shutdown -h now" when it is pressed?

Is this the case? The host system is Debian.

1

There are 1 best solutions below

1
KamilCuk On

run a script with "shutdown -h now" when it is pressed?

You can run halt which will immediately halt your system by sending reboot(LINUX_REBOOT_CMD_HALT systemd call to kernel. I/O operations will not be flushed, data may be lost. It's nice to call sync before it.

To gracefully shutdown your system, you have to communicate to your system manager the intent of shutting down.

Some time ago, shutdown was just sending a signal to PID 1 (i.e. it's communicating the intent of shutting down). In case of docker, it will kill PID 1 inside the container, so docker container will just be stopped.

In case of nowadays with systemd, you would have to send a dbus (or whatever systemd uses to communicate) message to PID 1 systedm process on your host.

The simplest would be to just ssh to your host and execute shutdown (or systemd-shutdown).

I think the much harder solution would be to access the DBUS from host and send the proper message on it.