Idle containers in Azure Container Instances

Azure Container Instances are the easiest way to host Docker containers in the cloud. Until you want them to do… nothing.

Running containers on Azure Container Instances

Azure Container Instances (ACI) is one of the services on Microsoft Azure that you can just start using with little to no overhead. You point it to a Docker image that you want to run, which can be either public or private, optionally configure a few details and off you go.

ACI takes away the complexity of setting up and operating Docker infrastructure and allows you to focus on your job. Sure, if you have a complex SaaS solution that consists of multiple containers that depend on one another, it might not be the right solution for you, but if all you need is a way to run a specific task, either on-demand or on a schedule, you will appreciate the simplicity of ACI.

ACI can be used for anything: from hosting web sites, APIs and web servers to running tasks that work with Azure Storage Queues, Table- and Blob Storage. As long as you have a Docker image that does something, you can start it in ACI. But what if you have an image that doesn’t do anything?

Running idle containers on ACI

Recently I’ve been researching the idea of running the Office 365 CLI in a Docker container that I could communicate with from a SharePoint Framework web part. I would run the container with the CLI in the cloud which would be sitting idle, waiting for me to send a command to it. ACI was the perfect candidate for the job for its ease of setup and use.

Typically, when you build a Docker image with which you want to interact, you specify /bin/bash as the image’s CMD. After starting the container with the -it arguments, your terminal is linked to the container and you can directly execute commands inside the container. Unfortunately, when you run such image in ACI, the container starts and stop immediately. Apparently, /bin/bash is not a process that keeps running when running the container on ACI.

So instead, if you need your container to run idly waiting for your input, you need to keep it busy doing something, even if it’s nothing. To do that, include in your Docker image the following bash script:

while :
  sleep 1

Then, change CMD in your image to CMD /bin/bash /usr/src/ so that at startup, the container will execute the script which will never finish.

Once your container starts, you can execute commands in it using the Azure CLI or the Azure REST API. Oh, and don’t forget to stop your container if you no longer need it to avoid incurring unnecessary costs.

Photo by frank mckenna

Others found also helpful: