通过Docker和Visual Studio发布到Azure的Azure函数HTTP请求404 https://aka.ms/containerfastmode

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

我正在尝试了解一些有关Azure函数2.0和要发布到我的Azure实例的Docker容器的信息。我按照下面的教程进行操作,唯一的区别是我使用Visual Studio 2019将docker与docker发布到azure中的容器注册表中。

https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-your-first-function-visual-studio

这一切都正常工作,我能够启动容器并访问该站点。但是,在示例中,您可以访问/ api / function1并获得响应。这在我的本地主机上有效,但在实际站点上它返回404。似乎/ api / function1在发布后无法访问。

应用本身在访问IP本身时会返回此值,因此我知道它正在运行。我需要在Azure中做其他事情来公开我的API吗?

Azure screenshot working default page

我的容器日志仅显示此。

Hosting environment: Production
Content root path: C:\
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

我从这里抓取了我的dockerfile

https://github.com/Azure/azure-functions-docker/blob/master/host/2.0/nanoserver-1809/Dockerfile

# escape=`

# Installer image
FROM mcr.microsoft.com/windows/servercore:1809 AS installer-env

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Retrieve .NET Core SDK
ENV DOTNET_SDK_VERSION 2.2.402

RUN Invoke-WebRequest -OutFile dotnet.zip https://dotnetcli.blob.core.windows.net/dotnet/Sdk/$Env:DOTNET_SDK_VERSION/dotnet-sdk-$Env:DOTNET_SDK_VERSION-win-x64.zip; `
    $dotnet_sha512 = '0fa3bf476b560c8fc70749df37a41580f5b97334b7a1f19d66e32096d055043f4d7ad2828f994306e0a24c62a3030358bcc4579d2d8d439d90f36fecfb2666f6'; `
    if ((Get-FileHash dotnet.zip -Algorithm sha512).Hash -ne $dotnet_sha512) { `
        Write-Host 'CHECKSUM VERIFICATION FAILED!'; `
        exit 1; `
    }; `
    `
    Expand-Archive dotnet.zip -DestinationPath dotnet; `
    Remove-Item -Force dotnet.zip

ENV ASPNETCORE_URLS=http://+:80 `
    DOTNET_RUNNING_IN_CONTAINER=true `
    DOTNET_USE_POLLING_FILE_WATCHER=true `
    NUGET_XMLDOC_MODE=skip `
    PublishWithAspNetCoreTargetManifest=false `
    HOST_COMMIT=69f124faed40d20d9d8e5b8d51f305d249b21512 `
    BUILD_NUMBER=12858

RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; `
    Invoke-WebRequest -OutFile host.zip https://github.com/Azure/azure-functions-host/archive/$Env:HOST_COMMIT.zip; `
    Expand-Archive host.zip .; `
    cd azure-functions-host-$Env:HOST_COMMIT; `
    /dotnet/dotnet publish /p:BuildNumber=$Env:BUILD_NUMBER /p:CommitHash=$Env:HOST_COMMIT src\WebJobs.Script.WebHost\WebJobs.Script.WebHost.csproj --output C:\runtime


# Runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2.7-nanoserver-1809

COPY --from=installer-env ["C:\\runtime", "C:\\runtime"]

ENV AzureWebJobsScriptRoot=C:\approot `
    WEBSITE_HOSTNAME=localhost:80

CMD ["dotnet", "C:\\runtime\\Microsoft.Azure.WebJobs.Script.WebHost.dll"]

这是我的天蓝色函数的我的function1代码

public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string productid = req.Query["productid"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            productid = productid ?? data?.product;

            Product newProduct = new Product()
            {
                ProductNumber = 0,
                ProductName = "Unknown",
                ProductCost = 0
            };
            if (Convert.ToInt32(productid) ==1)
            {
                newProduct = new Product()
                {
                    ProductCost = 100,
                    ProductName = "Lime Tree",
                    ProductNumber = 1
                };
            }
            else if(Convert.ToInt32(productid) == 2) 
            {
                newProduct = new Product()
                {
                    ProductCost = 500,
                    ProductName = "Lemon Tree",
                    ProductNumber = 2
                };
            }
            return productid != null
                ? (ActionResult)new JsonResult(newProduct)
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }

这是我的容器中装有我的图像的照片。

Container image

我是新来的,因此任何建议都可以肯定会有所帮助!

谢谢!

我正在尝试了解一些有关Azure函数2.0和要发布到我的Azure实例的Docker容器的信息。我跟随下面的教程,唯一的不同是我与...

azure docker azure-functions http-status-code-404 httpresponse
2个回答
1
投票

首先,我不知道您是否真的需要(或想要)在Windows容器上运行Functions。如果您想在容器中运行,我可能会选择Linux。为此,这是一个示例Dockerfile。它确实建立在Microsoft提供的基础映像之上。因此,您不必从头开始构建它。


0
投票

@@@ silent的答案是正确的-Linux容器是使用Azure Functions的方法。没有为Linux容器正确设置我的环境,但是一旦获得正确的环境,它就可以立即使用。

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