Search code examples
azuredockercrondocker-composevirtual-machine

Schedule Azure VM to turn on, run a script and turn off


I have a multi-container app that I need to run twice a day at specific times for about 30 mins each time. To save costs, I would like to:

  1. Turn on a Linux VM on Microsoft Azure
  2. Run a script that runs docker-compose up -d, waits 30 mins, then calls docker-compose stop
  3. Turn off the VM

I'd love for this to happen automatically on a cron schedule.

So far, I've failed to find any Azure solution that can do all of this. Azure automation allows you to turn on/off VMs but it doesn't allow you to run a sh script on it after you've turned it on. Azure Logic apps allows you to spin up containers on a schedule but not with docker-compose.

Any idea on how I might accomplish this in a way that doesn't involve me having a VM that is always on?


Solution

  • No worries , you can use this AZ powershell command to run sh script on your Azure VMs directly via Azure automation runbook with prarm : -CommandId 'RunShellScript'

    For AzureRM commands , refer to here

    I have tested on my side and it works perfectly for me .