首页 > 解决方案 > 使用 docker 将 microsoft bot 框架部署到 heroku 的问题

问题描述

我是 Docker 和 C# 的新手,正在尝试使用 Docker 将 microsoft bot 框架部署到 Heroku

我正在使用这个预先构建的示例:

https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/13.core-bot 我已使用正确的 Luis 凭据更新了应用程序设置。我用这个端点创建了一个 azure bot 服务:https://mybotfram.herokuapp.com/api/messages

Then i updated the bot service credential in the app setting json. 

{
  "MicrosoftAppId": "****************************************",
  "MicrosoftAppPassword": "********************",
  "LuisAppId": "********************",
  "LuisAPIKey": "********************",
  "LuisAPIHostName": "westus.api.cognitive.microsoft.com"
}

我的文件夹有这样的外观:

在此处输入图像描述

然后到正确的文件夹:

heroku container:login
heroku create myrepeat
heroku container:push web --app mybotfram
heroku container:release web --app mybotfram

我的 Dockerfile 看起来像这样:


FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY ./*.csproj .
RUN dotnet restore

# copy everything else and build app
COPY ./. .
WORKDIR /app
RUN dotnet publish -c Release -o out


FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
WORKDIR /app
# COPY ./*.bot ./
COPY --from=build /app/out ./
ENTRYPOINT ["dotnet", "CoreBot.dll"]

端点不起作用,我在 Heroku 日志中收到此错误,似乎没有任何工作正常。

2019-10-30T15:23:32.220067+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/api/messages" host=mybotfram.herokuapp.com request_id=edda2c79-ab2e-4258-8a17-38a145f06f06 fwd="13.94.246.37" dyno= connect= service= status=503 bytes= protocol=https
2019-10-30T15:25:52.90006+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/api/messages" host=mybotfram.herokuapp.com request_id=387e5fcf-7330-45f9-8751-d3c156618565 fwd="13.94.246.37" dyno= connect= service= status=503 bytes= protocol=https
2019-10-30T15:33:14.734976+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=mybotfram.herokuapp.com request_id=5b7a5fe7-9371-4911-a563-d3aeba8411c4 fwd="84.101.210.119" dyno= connect= service= status=503 bytes= protocol=https
2019-10-30T15:33:14.383936+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/api/messages" host=mybotfram.herokuapp.com request_id=75836fb3-afca-4725-9b00-1ffcdec0e2a1 fwd="84.101.210.119" dyno= connect= service= status=503 bytes= protocol=https

如前所述,我对此并不陌生,因此将不胜感激任何有关上述内容的见解!


_________________________________________编辑__________________________________


在回答 Mdrichardson 之后进行编辑(非常感谢!)。

这是我根据您的输入重写我的 Program.cs 的方式。

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using System;

namespace Microsoft.BotBuilderSamples
{
    public class Program
    {
        public static void Main(string[] args)
                {
                    CreateWebHostBuilder(args)
                        .UseKestrel()
                        .UseUrls("http://0.0.0.0:" + Environment.GetEnvironmentVariable("PORT"))
                        .Build()
                        .Run();
                }


        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureLogging((logging) =>
                {
                    logging.AddDebug();
                    logging.AddConsole();
                })
                .UseStartup<Startup>();
    }
}

标签: c#azuredockerherokubotframework

解决方案


There's a full how-to below this section

For you, specifically:

503 errors usually indicate your client can't communicate with the bot.

My guess is that you need to update your Program.cs to contain this:

public static void Main(string[] args)
{
    CreateWebHostBuilder(args)
        .UseKestrel()
        .UseUrls("http://0.0.0.0:" + Environment.GetEnvironmentVariable("PORT"))
        .Build()
        .Run();
}

Heroku provides the exposed port via the PORT environment variable. Your bot just needs to listen on it.

If that doesn't work for you, I recommend starting from scratch and following the tutorial below


Got this working, from scratch.

How to Dockerize Your Bot and Deploy to Heroku

The majority of the credit for this should go to Victor Reyes for this article, since I basically did the same thing, but with a bot.

The following tutorial will cover the basics and assumes you know a bit about developing bots. See the article for more details on the Docker/Heroku side and our official docs for the bot side of things.

1. Download CoreBot.

2. Update appsettings.json with your bot and LUIS information.

3. Run the app locally and verify that it works in Emulator. This tests that your bot works at all.

4. Create a dockerfile in the root folder with your .csproj. It should look like this:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2.300-alpine3.9 as server

ENV ASPNETCORE_Environment=Production
ENV ASPNETCORE_URLS http://+:3978

WORKDIR /server
VOLUME ./wwwroot/Repository
COPY . ./

RUN dotnet publish -c Release -o publish

EXPOSE 3978/tcp

ENTRYPOINT ["dotnet","publish/CoreBot.dll"]

5. From the root folder with your .csproj file, run docker build -t <dockerImageName>:<tag> .

6. Run docker run -p 3978:3978 <dockerImageName>:<tag>

7. Run the app locally and verify that it works in Emulator. This tests that your bot works as a Docker image.

8. Update your dockerfile so that it now looks like this:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2.300-alpine3.9 as server

ENV ASPNETCORE_Environment=Production

WORKDIR /server
COPY . ./

RUN dotnet publish -c Release -o publish

ENTRYPOINT ["dotnet","publish/CoreBot.dll"]

9. Overwrite Main() in Program.cs so that it contains:

public static void Main(string[] args)
{
    CreateWebHostBuilder(args)
        .UseKestrel()
        .UseUrls("http://0.0.0.0:" + Environment.GetEnvironmentVariable("PORT"))
        .Build()
        .Run();
}

10. Run the following:

heroku login
heroku container:login
docker login --username=_ --password=$(heroku auth:token) registry.heroku.com

11. Create a Heroku App and remember its

12. Run:

docker build -t registry.heroku.com/<appName>/web .
docker push registry.heroku.com/<appName>/web
heroku container:release web --app <appName>

13. Test in Emulator by using https://<appName>.herokuapp.com/api/messages and your MicrosoftAppId and MicrosoftAppPassword

Note: Depending on your environment you may need to use 0.0.0.0:3978 as opposed to localhost:3978. It may also be worth trying 127.0.0.1:3978.


推荐阅读