Azure Functions on Docker

Harshit Anand
3 min readNov 19, 2019

--

Azure Functions and Docker are a perfect match for microservices in a hybrid world. This combo gives you the flexibility to easily deploy and run your microservices either in the cloud or on-premises. While I prefer Azure as a cloud platform, you are free to host the Docker Functions on any other cloud platform.

Microsoft already provides some Docker base images for hosting Azure Functions in a Linux container. Besides the actual base image, there are some platform-specific images for Dotnet core, node, and the python. Because all of these images don’t support SSH out of the box and the node image only comes with node 8, I’ve decided to build my own base image with support for SSH and node 10.

The Function Base Image

GitHub: https://github.com/ltwlf/azure-functions-docker-node
Docker Hub: https://hub.docker.com/r/leitwolf/azure-functions-node/

“Hello Docker”-Function

Now we are going to use our new base image (you can use mine on Docker Hub or publish your own image as described above) and write a simple “Hello Docker”-Function example:

  • Install the Azure Function Core Tools:
    npm i -g azure-functions-core-tools@core --unsafe-perm true
  • Create a new folder “Hello-Docker-Function”
  • Run the following commands:
    func init --language JavaScript --docker --worker-runtime JavaScript
    func new --name Hello -l JavaScript --template HttpTrigger
  • Change the auth level in function.json to:
    authLevel="anonymous"
    (I will cover in another post how you can use function key authentication in Docker functions in another post - it's tricky)
  • Change the Dockerfile to:
FROM leitwolf/azure-functions-node
ENV AzureWebJobsScriptRoot=/home/site/wwwroot
COPY . /home/site/wwwroot
  • Build a Docker image:
    docker build --rm -f "Dockerfile" -t hello-docker-function:latest .
  • Run the Docker container:
    docker run --rm -it -p 2222:2222/tcp -p 80:80/tcp -p 8000:8000/tcp hello-docker-function:latest
  • Browse to “http://localhost/api/hello?name=Docker Function!”

Here are the sources on GitHub

Awesome! Our Azure Functions is now running in a local docker container :)

Deploy to Azure

First, you have to push our Container to Docker Hub (or to the Azure Container Registry (ACR)).

Run the following commands:
docker tag hello-docker-function YOURACCOUNT/hello-docker-function
docker push YOURACCOUNT/hello-docker-function:latest

Here you can find the image already on Docker Hub

Now we can deploy our Azure Function on Azure. Browse to the Azure Portal and create a new “Function App”, choose “Docker” and the image that we have published to Docker Hub. In production scenarios you would rather choose private, Azure Container Registry or the Private Registry, but for our demo, public Docker Hub is just fine:

Create…

Let’s test our Docker Function

After a few minutes, the Azure Function should be ready. In the following clip, I’ll show you how you can test the function, view the logs and how to use the BASH and SSH shell. It is important to notice that BASH actually runs on the Docker Host while SSH runs within in the container!

--

--

Harshit Anand
Harshit Anand

Written by Harshit Anand

I am a programmer, learner, tech enthusiast, discovering possibilities with product & tech

No responses yet