|
| 1 | +# Quickstart Function Samples as a Custom image with Docker |
| 2 | + |
| 3 | +This is a quickstart on how you start running Python function samples as a custom image (Container) with Docker. |
| 4 | + |
| 5 | +<!-- TOC --> |
| 6 | +- [Quickstart Function Samples as a Custom image with Docker](#quickstart-function-samples-as-a-custom-image-with-docker) |
| 7 | + - [Prerequisites](#prerequisites) |
| 8 | + - [Git clone source code](#git-clone-source-code) |
| 9 | + - [Create Azure Resources that required to run the samples](#create-azure-resources-that-required-to-run-the-samples) |
| 10 | + - [[Required] Azure Storage Account](#required-azure-storage-account) |
| 11 | + - [[Optional] CosmosDB and Computer Vision API](#optional-cosmosdb-and-computer-vision-api) |
| 12 | + - [Build Container Image](#build-container-image) |
| 13 | + - [Run the image locally](#run-the-image-locally) |
| 14 | + - [1. Run the image with minimum configuration](#1-run-the-image-with-minimum-configuration) |
| 15 | + - [2. Run the image with full configuration](#2-run-the-image-with-full-configuration) |
| 16 | + - [Test access to the functions](#test-access-to-the-functions) |
| 17 | + - [LINKS](#links) |
| 18 | + |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | +- [Docker](https://docs.docker.com/) |
| 22 | + |
| 23 | +## Git clone source code |
| 24 | +```sh |
| 25 | +git clone https://github.com/yokawasa/azure-functions-python-samples.git |
| 26 | +``` |
| 27 | + |
| 28 | +## Create Azure Resources that required to run the samples |
| 29 | + |
| 30 | +This project include a set of multiple sample function and each function may have different required resources. Please check `readme.md` included in each function sample (Check [v2functions](../v2functions)). |
| 31 | + |
| 32 | +### [Required] Azure Storage Account |
| 33 | +A minimum is an `Azure Storage Account` which is necessary for all functions. Here is how you create: |
| 34 | + |
| 35 | +> [scripts/create-resource-group.sh](../scripts/create-resource-group.sh) |
| 36 | +```bash |
| 37 | +RESOURCE_GROUP="<RESOURCE GROUPP MAME>" |
| 38 | +REGION="<REGION NAME: eastus>" |
| 39 | +az group create --name $RESOURCE_GROUP --location $REGION |
| 40 | +``` |
| 41 | +> [scripts/create-storage-account.sh](../scripts/create-storage-account.sh) |
| 42 | +```bash |
| 43 | +RESOURCE_GROUP="<RESOURCE GROUPP MAME>" |
| 44 | +REGION="<REGION NAME: eastus>" |
| 45 | +STORAGE_ACCOUNT="<STORAGE ACCOUNT NAME>" |
| 46 | + |
| 47 | +echo "Create an Azure Storage account: $STORAGE_ACCOUNT" |
| 48 | +az storage account create --name $STORAGE_ACCOUNT \ |
| 49 | + --location $REGION \ |
| 50 | + --resource-group $RESOURCE_GROUP \ |
| 51 | + --sku Standard_LRS |
| 52 | +``` |
| 53 | + |
| 54 | +### [Optional] CosmosDB and Computer Vision API |
| 55 | +The rest of resources such as Cosmos DB account and Computer Vision Subscription are optionals: |
| 56 | + |
| 57 | +For CosmosDB Account and its database and collections, you can leverage the following helper script. Adding required params in the script and running will create a CosmosDB Account and database and collections. |
| 58 | +> [scripts/create-cosmosdb-test-db-coll.sh](../scripts/create-cosmosdb-test-db-coll.sh) |
| 59 | +
|
| 60 | +For Computer Vision API subscription, you can leverage the following helper script. Likewise, add required params in the script and run it. |
| 61 | +> [scripts/create-cognitive-computer-vision.sh](../scripts/create-cognitive-computer-vision.sh) |
| 62 | +
|
| 63 | +## Build Container Image |
| 64 | + |
| 65 | +Let's build the image from the Docker file using `docker build` command. |
| 66 | + |
| 67 | +```bash |
| 68 | +cd v2functions |
| 69 | + |
| 70 | +# Build the image with `docker build` command |
| 71 | +# docker build --tag <docker-id>/<imagename>:<tag> . |
| 72 | +docker build --tag yoichikawasaki/azfuncpythonsamples:v0.0.1 . |
| 73 | +``` |
| 74 | +You can also use a helper script - [scripts/docker-build.sh](../scripts/docker-build.sh) |
| 75 | + |
| 76 | +## Run the image locally |
| 77 | + |
| 78 | +Now you're ready to run the app. You have 2 options |
| 79 | + |
| 80 | +### 1. Run the image with minimum configuration |
| 81 | + |
| 82 | +> [scripts/docker-run-mini.sh](../scripts/docker-run-mini.sh) |
| 83 | +```bash |
| 84 | +... |
| 85 | +docker run -p 8080:80 -it \ |
| 86 | + -e AzureWebJobsStorage="$STORAGE_CONNECTION_STRING" \ |
| 87 | + $DOCKER_ID/$CONTAINER_IMAGE_NAME:$TAG |
| 88 | +... |
| 89 | +``` |
| 90 | + |
| 91 | +### 2. Run the image with full configuration |
| 92 | + |
| 93 | +> [scripts/docker-run.sh](../scripts/docker-run.sh) |
| 94 | +```bash |
| 95 | +... |
| 96 | +docker run -p 8080:80 -it \ |
| 97 | + -e AzureWebJobsStorage="$STORAGE_CONNECTION_STRING" \ |
| 98 | + -e MyStorageConnectionString="$STORAGE_CONNECTION_STRING" \ |
| 99 | + -e MyCosmosDBConnectionString="$COSMOSDB_CONNECTION_STRING" \ |
| 100 | + -e ComputerVisionSubscription="$COMPUTER_VSION_API_SUBSCRIPTION" \ |
| 101 | + -e ComputerVisionApiEndpoint="$COMPUTER_VSION_API_ENDPOINT" \ |
| 102 | + $DOCKER_ID/$CONTAINER_IMAGE_NAME:$TAG |
| 103 | +... |
| 104 | +``` |
| 105 | + |
| 106 | +## Test access to the functions |
| 107 | + |
| 108 | +Once you start the app with docker, let's send a test request to `http-trigger-dump-request` function: |
| 109 | + |
| 110 | +```bash |
| 111 | +curl -s http://localhost:8080/api/http-trigger-dump-request |jq |
| 112 | + |
| 113 | +{ |
| 114 | + "method": "GET", |
| 115 | + "url": "http://localhost:8080/api/http-trigger-dump-request", |
| 116 | + "headers": { |
| 117 | + "accept": "*/*", |
| 118 | + "host": "localhost:8080", |
| 119 | + "user-agent": "curl/7.54.0" |
| 120 | + }, |
| 121 | + "params": {}, |
| 122 | + "get_body": "" |
| 123 | +} |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +## LINKS |
| 128 | +- [Create a function on Linux using a custom image](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image) |
0 commit comments