Shutting down the the Apalis SoM from inside a docker container

Hi Toradex,

We are running a UI inside a docker container. One of the requirements is that users can cleanly shut down the device from the UI, however this is not possible from inside a docker container.

I’ve implemented workarounds for this exact problem in the past by having another process running on the host which handles the shut down on behalf of the container, however this involves maintaining applications outside of the docker environment which we would like to avoid. I realise there are also a bunch people on stack overflow with the same question and the answer is usually something similar to the above.

My question is whether you have a recommended way of shutting down SoMs running Torizon? A button on the UI would be good but a hardware button would also work.

Thanks in advance

Greetings @steve-ir,

After some brief research, this does seem possible. However, from what I understand you’ll need to create an interface from the container to the host systems. This kind of breaks the whole concept of containers however and all the solutions I found for this seem to be on the “hacky” side, for example this forums discussion: Is it possible to shut down the host machine by executing a command on one of its docker container? - Stack Overflow

This is cause, ideally you’d want a clean and proper shutdown of all systems rather than a forced off scenario. Which is why you need either a proper interface to the host or maybe the container would need root privileges or such, which can also be problematic in practice.

I’ll continue to look and maybe consult the team here a bit if there’s a better solution.

Best Regards,
Jeremias

Just thought of this after the fact, but another interesting approach here would be to use localhost and SSH from the container to the host and execute a shutdown command that way. In this way you’d be running the shutdown command on the host directly without need a special host-container interface.

This does however bring up the issue that you’d need SSH in your container as well as credentials to do the SSH.

here is an example mentioned by Jeremias approach

wait_for_shutdown.service file will be used to control the service

[Unit]
Description=Wait for the signal to shutdown system through a shared file

[Service]
Restart=on-failure
Type=simple
ExecStart=/opt/app-system/wait_for_shutdown.sh
RestartSec=5

[Install]
WantedBy=multi-user.target

wait_for_shutdown.sh file waits for a signal in the shared file

#!/bin/sh

echo "start wait_for_shutdown service"
echo "waiting" > /var/run/shutdown_signal
echo "start wait_for_shutdown service while-do"
while sleep 1; do 
  signal=$(cat /var/run/shutdown_signal)
  if [ "$signal" == "true" ]; then 
    echo "wait_for_shutdown service - received shutdown"
    echo "done" > /var/run/shutdown_signal
    shutdown -h now
  fi
done
echo "end wait_for_shutdown service while-do"

wait_for_shutdown_service_install.sh file - use this script to install the service

#!/bin/sh

mkdir -p /opt/app_system
cp ./wait_for_shutdown.sh /opt/app_system
sudo cp ./wait_for_shutdown.service /etc/systemd/system
sudo systemctl enable wait_for_shutdown.service
sudo systemctl start wait_for_shutdown.service
1 Like

Thank you for sharing your approach.

Hello, I was trying to implement the suggested scripts to shutdown a Verdin iMX8M Plus. I installed the wait_for_shutdown_service and was trying to write to the shutdown_signal file and somehow I’ve lost mount and apt commands. When I reboot the container still starts and I can access an exfat flash from the container but I can’t access it when I log in from teraterm and I’m not in the container. I tried to run mount and realized I don’t have mount and then I realized I also don’t have apt. Do you have a suggestion for how to get them back? Do I need to reinstall the OS?

Hi @hmaquet,

This thread is a bit dated and as such information here might no longer be accurate or applicable anymore. Could you open a new thread with your question/issue and we can try to assist you with hopefully more up-to-date information.

Best Regards,
Jeremias