Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Building Python Functions App that use Native Modules – Custom Docker

Prerequisites

To build and test locally, you will need to:

  • Install Python 3.6
  • Install Docker
  • Install Azure Functions Core Tools version 2.0.3 or later

To install Azure Functions Core tools you will need also these requirements:

  1. Install .NET Core 2.1 for Windows.
  2. Install Node.js, which includes npm. For version 2.x of the tools, only Node.js 8.5 and later versions are supported.
  3. Install the Core Tools package:
    npm install -g azure-functions-core-tools

Create and activate a virtual environment

To create a Functions project, it is required that you work in a Python 3.6 Virtual Environment. Run the following commands to create and activate a virtual environment named env.

# In Bash
python3.6 -m venv env
source env/bin/activate

# In PowerShell
py -3.6 -m venv env
envscriptsactivate

# In CMD
python -m venv env
envscriptsactivate

Create a local Functions Project

To create a Functions project, it is required that you work in a Python 3.6 virtual environment. Run the following commands to create and activate a virtual environment named env. In the terminal window or from a command prompt, run the following command:

func init MyFunctionProj --worker-runtime python –docker
func init . --worker-runtime python --docker

You will see something like the following output.

Installing wheel package
Installing azure-functions==1.0.0a5 package
Installing azure-functions-worker==1.0.0a6 package
Running pip freeze
Writing .funcignore
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing D:__DocumentsWebAppProjectsfunctions-python-3.vscodeextensions.json
Writing Dockerfile

This generates a Dockerfile with the following content:

FROM mcr.microsoft.com/azure-functions/python:2.0

COPY . /home/site/wwwroot

RUN cd /home/site/wwwroot &&

    pip install -r requirements.txt

Create a function

https://github.com/Azure/azure-functions-python-worker/wiki/Create-your-first-Python-function#create-a-function

Run the function locally

https://github.com/Azure/azure-functions-python-worker/wiki/Create-your-first-Python-function#run-the-function-locally

Run locally as Docker container

Using Docker, run the following command.
Replace and with preferred names.

docker build . --tag :
(example) docker build . -- pythonfunction:v1

Once image has been build you could run the following command to start the image:

docker run -p ::
(example) docker run -p 8080:80 pythonfunction:v1

Once started, you could browse to http://localhost:8080 (using example) to view your function running.

Deploy your function app

You could use Docker Hub or Azure Container Registry to deploy your Docker image.

Push image to Docker Hub

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#push-image-to-acr

First Create a container registry

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#create-a-container-registry

Log in to ACR

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#create-a-container-registry

Push image to ACR

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#push-image-to-acr

Create function app via Azure Portal

Select the New button found on the upper left-hand corner of the Azure portal, then select Compute > Function App.

When configuring the settings, select Linux (Preview) for OS and Docker Image for Publish.

Under Configure Container, select either Azure container Registry or Docker Hub depending on how you deployed your Docker image.

Once deployed, you will be able to access the Function Apps page

Once deployed, you can test your Function App:

Be sure to change and with your functions values.

https://.azurewebsites.com/api/

Share the post

Building Python Functions App that use Native Modules – Custom Docker

×

Subscribe to Msdn Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The Msdn Blogs.

Get updates delivered right to your inbox!

Thank you for your subscription

×