Azure Functions on Docker - Getting started (Part 1)

Azure Functions and Docker are a perfect match for micro services in a hybrid world. This combo gives you the flexibility to easily deploy and run your micro services either in the cloud or on-premises. While I prefer Azure as 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. Beside 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

Docker Hub:

"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:

  1. Install the Azure Function Core Tools:
    npm i -g azure-functions-core-tools@core --unsafe-perm true
  2. Create a new folder "Hello-Docker-Function"
  3. Run the following commands:
    func init --language JavaScript --docker --worker-runtime JavaScript
    func new --name Hello -l JavaScript --template HttpTrigger
  4. Change the authLevel in function.json to:
    (I will cover in a other post how you can use function key authentication in Docker functions in another post - it's tricky)
  5. Change the Dockerfile to:
    FROM leitwolf/azure-functions-node
    ENV AzureWebJobsScriptRoot=/home/site/wwwroot
    COPY . /home/site/wwwroot
  6. Build Docker image:
    docker build --rm -f "Dockerfile" -t hello-docker-function:latest .
  7. Run the Docker container:
    docker run --rm -it -p 2222:2222/tcp -p 80:80/tcp -p 8000:8000/tcp hello-docker-function:latest
  8. 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 an 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:


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!

In the next posts we will dive deeper into hosting Azure Function in Docker, for example programmatic provisioning, continuous integration, security and on-premises...