为什么我的Python应用程序在进行代码部署时可以工作,但在使用docker镜像时却可以工作

问题描述 投票:0回答:1

我使用代码部署到azure部署了一个http触发功能,它的工作功能被显示并响应欣赏数据。

当我创建 docker 映像时,该映像在本地工作,但当我部署到 azure 时,没有任何功能。并尝试执行该函数会导致 404 错误。

我比较了环境变量,两个功能应用程序之间的环境变量是相同的。

我过去两天阅读了有关此问题的博客,但没有找到任何真正的解决方案。

我错过了什么?

这是我的 Docker 文件:

# Base image with Python 3.9
FROM mcr.microsoft.com/azure-functions/python:4-python3.12

# Set environment variables to avoid writing .pyc files and buffering output
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

# Install git to clone the Bitbucket repository
RUN apt-get update && apt-get install -y git 


 ENV AZURE_FUNCTIONS_ENVIRONMENT=Development

#Copy secrets file
RUN mkdir /etc/secrets/
ENV FUNCTIONS_SECRETS_PATH=/etc/secrets
ENV AzureWebJobsSecretStorageType=Files
ADD host_secrets.json /etc/secrets/host.json

# Install dependencies from requirements.txt
COPY requirements.txt /
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code (optional, if you want to add any additional files or scripts)
COPY . /home/site/wwwroot

这是我的秘密文件:

{
    "masterKey": {
        "name": "master",
        "value": "p##########################################################",
        "encrypted": false
    },
    "functionKeys": [{
        "name": "default",
        "value": "w##########################################################",
        "encrypted": false
    }]
}

这是我的环境变量:

[
  {
    "name": "APP_CONFIGURATION_LABEL",
    "value": "production",
    "slotSetting": false
  },
  {
    "name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
    "value": "InstrumentationKey=a###################################;IngestionEndpoint=https://a###################.in.applicationinsights.azure.com/;LiveEndpoint=https://a################.livediagnostics.monitor.azure.com/;ApplicationId=d######################################",
    "slotSetting": false
  },
  {
    "name": "AzureFunctionsJobHost__Logging__Console__IsEnabled",
    "value": "true",
    "slotSetting": false
  },
  {
    "name": "AzureWebJobsDashboard",
    "value": "DefaultEndpointsProtocol=https;AccountName=a########################;AccountKey=q################################################################;EndpointSuffix=core.windows.net",
    "slotSetting": false
  },
  {
    "name": "AzureWebJobsScriptRoot",
    "value": "/home/site/wwwroot",
    "slotSetting": false
  },
  {
    "name": "AzureWebJobsStorage",
    "value": "DefaultEndpointsProtocol=https;AccountName=a#############################;AccountKey=q###############################################################################;EndpointSuffix=core.windows.net",
    "slotSetting": false
  },
  {
    "name": "DOCKER_ENABLE_CI",
    "value": "true",
    "slotSetting": false
  },
  {
    "name": "DOCKER_REGISTRY_SERVER_PASSWORD",
    "value": "G########################################################",
    "slotSetting": false
  },
  {
    "name": "DOCKER_REGISTRY_SERVER_URL",
    "value": "https://#################.azurecr.io",
    "slotSetting": false
  },
  {
    "name": "DOCKER_REGISTRY_SERVER_USERNAME",
    "value": "############",
    "slotSetting": false
  },
  {
    "name": "FUNCTIONS_EXTENSION_VERSION",
    "value": "~4",
    "slotSetting": false
  },
  {
    "name": "FUNCTIONS_WORKER_RUNTIME",
    "value": "python",
    "slotSetting": false
  },
  {
    "name": "UseExpressBuild",
    "value": "/tmp/.cache",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
    "value": "#############################################################################",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_CONTENTSHARE",
    "value": "a#####################",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_ENABLE_SYNC_UPDATE_SITE",
    "value": "true",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_HEALTHCHECK_MAXPINGFAILURES",
    "value": "10",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_HTTPLOGGING_RETENTION_DAYS",
    "value": "2",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_RUN_FROM_PACKAGE",
    "value": "1",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_TIME_ZONE",
    "value": "Australia/Sydney",
    "slotSetting": false
  },
  {
    "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
    "value": "false",
    "slotSetting": false
  }
]

查看日志显示docker镜像已成功部署。 这是日志:

025-01-17T04:51:32.535Z INFO - c861b3c01817 Extracting 33MB / 33MB
2025-01-17T04:51:32.643Z INFO - c861b3c01817 Pull complete
2025-01-17T04:51:32.678Z INFO - Digest: sha256:3#########################################################################
2025-01-17T04:51:32.683Z INFO - Status: Downloaded newer image for azuretestcreg.azurecr.io/azfunctiontest:latest
2025-01-17T04:51:32.699Z INFO - Pull Image successful, Time taken: 45 Seconds
2025-01-17T04:51:32.722Z INFO - Starting container for site
2025-01-17T04:51:32.727Z INFO - docker run -d -p 9131:80 --name austeamtest3_0_89c7e566 -e WEBSITE_USE_DIAGNOSTIC_SERVER=false -e WEBSITE_CORS_ALLOWED_ORIGINS=https://portal.azure.com -e WEBSITE_CORS_SUPPORT_CREDENTIALS=False -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=austeamTest3 -e WEBSITE_AUTH_ENABLED=False -e PORT=80 -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=test6783.azurewebsites.net -e WEBSITE_INSTANCE_ID=5######################################################################### -e HTTP_LOGGING_ENABLED=1 testcreg.azurecr.io/azfunctiontest:latest
2025-01-17T04:51:35.762Z INFO - Initiating warmup request to container austeamtest3_0_89c7e566_msiProxy for site test6783
2025-01-17T04:51:35.825Z INFO - Container test6783_0_89c7e566_msiProxy for site austeamtest3 initialized successfully and is ready to serve requests.
2025-01-17T04:51:35.830Z INFO - Initiating warmup request to container austeamtest3_0_89c7e566 for site test6783
2025-01-17T04:51:40.439Z INFO - Container test6783_0_89c7e566 for site test6783 initialized successfully and is ready to serve requests.
2025-01-17T04:51:40.445Z INFO - Initiating warmup request to container austeamtest3_0_89c7e566_middleware for site test6783
2025-01-17T04:51:41.259Z INFO - Container test6783_0_89c7e566_middleware for site austeamtest3 initialized successfully and is ready to serve requests.

我已经阅读了这方面的博客,尝试添加博客中提到的所有环境变量。

我查看了日志并尝试使用 azure 诊断,但它没有给我任何信息。

需要列出函数并能够调用函数。

docker azure-functions containers azure-deployment
1个回答
0
投票

我已经提到了创建您的第一个容器化Azure Functions,在VS代码中本地创建http触发函数,然后使用Azure容器注册表部署到Azure函数应用程序。

创建函数时,它将创建如下所示的 docker 文件。

FROM mcr.microsoft.com/azure-functions/python:4-python3.7

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY requirements.txt /
RUN pip install -r /requirements.txt

COPY . /home/site/wwwroot

然后,使用以下命令构建并运行 docker 映像。

docker build --tag <DOCKER_ID>/azurefunctionsimage:v1.0.0 .
docker run -p 8080:80 -it <DOCKER_ID>/azurefunctionsimage:v1.0.0

本地成功执行后,您可以使用以下命令将代码部署到Azure容器注册表。

az acr login --name <REGISTRY_NAME>
docker tag <DOCKER_ID>/azurefunctionsimage:v1.0.0 <LOGIN_SERVER>/azurefunctionsimage:v1.0.0
docker push <LOGIN_SERVER>/azurefunctionsimage:v1.0.0

然后,我创建了一个具有高级计划的功能应用程序并选择了容器图像,如下所示。

enter image description here

在部署中心添加注册表详细信息。

enter image description here

几分钟后,您应该能够看到函数端点。

enter image description here

我能够成功执行函数应用程序中的函数。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.