Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to shut down the host machine by executing a command on one of its docker container?

Tags:

docker

I have a host machine which has one docker container. The container is active and running a particular service. On meeting a particular condition, I want to remove the container and shut down the machine also. Is it possible to do so? I am planning to modify the code which runs the service to handle the shutting down of the machine? Any suggestions are welcome!

like image 797
josepainumkal Avatar asked Mar 08 '17 00:03

josepainumkal


People also ask

How do I shut down my hosting machine?

Select the ESXi host you want to power off. Select Actions > Power. Select the operation. To power off and restart the ESXi host, click Reboot.

How do I turn off my docker machine?

you can run : Stop All docker machines : docker-machine stop $(docker-machine ls --format "{{. Name}}") (edited)

Which command shuts down a container gracefully?

docker rm -f The final option for stopping a running container is to use the --force or -f flag in conjunction with the docker rm command. Typically, docker rm is used to remove an already stopped container, but the use of the -f flag will cause it to first issue a SIGKILL.

How do I run a command in a docker container?

If you need to run a command inside a running Docker container, but don't need any interactivity, use the docker exec command without any flags: docker exec container-name tail /var/log/date.


1 Answers

Running a clean shutdown will be dependent on the hosts init system.

To avoid giving the container --privileged access and to also avoid installing host specific init tools in your container, you could create an interface to signal the host to shutdown rather than the trying to get the container to run the shutdown.

An Interface

There's many ways this could be done. A simple starting point could be a mounted volume to share data between the container and host. A file will do for now but you could use a socket, fifo, TCP or any other IPC method you want.

Create a file on the host, say /var/run/shutdown_signal and mount the file into your container

docker run -d -v /var/run/shutdown_signal:/shutdown_signal whatever 

Write a string into the file when you want the host to shutdown

docker exec $cid sh -c 'echo true > /shutdown_signal'

Then you need something running on the host to monitor the file.

A simple script that waits for file changes with inotifywait.

echo "waiting" > /var/run/shutdown_signal
while inotifywait -e close_write /var/run/shutdown_signal; do 
  signal=$(cat /var/run/shutdown_signal)
  if [ "$signal" == "true" ]; then 
    echo "done" > /var/run/shutdown_signal
    shutdown -h now
  fi
done

You could poll the file if inotifywait is not available.

while sleep 30; do
  signal=$(cat /var/run/shutdown_signal)
  ...

The Horrible Alternative

There is also a more universal, kernel way to trigger an immediate, unclean shutdown in Linux.

docker run --privileged busybox \
  sh -c 'echo 1 > /proc/sys/kernel/sysrq; echo o > /proc/sysrq-trigger'

But this will likely cause more issues for you than it's worth.

like image 67
Matt Avatar answered Sep 23 '22 23:09

Matt