6

I am trying to deploy a container on a Compute Engine VM (using the create-with-container parameter).

The container runs a single long running task and exits. How can I shut down the VM when the container exits?

[Edit: the workload is implemented in Java and is memory intensive, hence the choice of Compute Engine rather than a managed service like App Engine or Cloud Functions]

daphshez
  • 9,272
  • 11
  • 47
  • 65
  • 2
    I would create a simple web server, for example written in Python and Flask, that runs on startup inside the instance. Create a shared secret key that the Flask web server and the container know. Your software inside the container calls the endpoint. The endpoint calls `halt()`. The VM then shuts down. This would be a total of maybe 20 lines of code. The key is that the Flask web server will need to run with privilege to call halt(). – John Hanley Nov 30 '18 at 10:47
  • @JohnHanley This seems to be a reasonable answer (if not as straight forward as I had hoped for) - if you post it as an answer I'll be able to upvote it. – daphshez Nov 30 '18 at 23:37
  • My favourite solution is now here: https://stackoverflow.com/a/58215421/79332 – daphshez Oct 03 '19 at 08:49

2 Answers2

1

I would create a simple web server, for example written in Python and Flask, that runs on startup inside the instance.

Create a shared secret key that the Flask web server and the container know to provide security.

Your software inside the container calls the endpoint. The endpoint calls halt(). The VM then shuts down. This would be a total of maybe 20 lines of code.

The key is that the Flask web server will need to run with privilege to call halt().

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • I'm voting this up because I a startup script is a reasonable approach, if not ideal (debugging those startup scripts is pain, for once). However I wouldn't want to rely on a request from the process inside the container, because this process may terminate abnormally. I would maybe go for setting up a cronjon that checks I'd the docker process is still alive. – daphshez Dec 04 '18 at 08:36
0

Either use Cloud Functions for such workloads, schedule task with Cron for Python using App engine task queues or leverage Cloud Scheduler, which can run bash script defining logic of spinning-up and killing VM. I'm sure with one of these you can solve your use-case. I'd go for Cloud Functions first.

Matus Cimerman
  • 417
  • 2
  • 10