heroku_将dockerized ASP.NET Core应用程序部署到Heroku的实用性

heroku

介绍 (Intro)

.NET is a relative newcomer in the open-source world, and its popularity is nowhere near mainstream platforms like Node.js. So you can imagine there're few tutorials that deal with .NET and frameworks such as ASP.NET on Heroku. And those that do, probably won't use containers.

.NET在开放源代码世界中是一个相对较新的应用程序,它的流行远不及主流平台(如Node.js)。 因此,您可以想象很少有教程涉及.NET和Heroku上的ASP.NET等框架。 而那些容器可能不会使用容器。

Image showing heroku menu without C#

Do you see C#/.NET here? Yes, me neither.

您在这里看到C#/。NET吗? 是的,我也没有。

入门 (Getting started)

This tutorial will assume you have Docker, .NET Core and Heroku tools installed. I will use Linux (Ubuntu), but AFAIK those tools are cross-platform so the steps will be the same for any supported OS.

本教程将假定您已安装Docker,.NET Core和Heroku工具。 我将使用Linux(Ubuntu),但是AFAIK这些工具是跨平台的,因此对于任何受支持的OS来说,步骤都是相同的。

Let's take the easiest case — simple MVC app. If you don't have one, just create it by running

让我们以最简单的情况为例-简单的MVC应用程序。 如果您没有,只需通过运行创建它

dotnet new mvc --name mymvc

I'll also assume you have a dockerfile ready, maybe something like proposed in this tutorial:

我还要假设您已经准备好一个dockerfile,也许类似于本教程中所建议的:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS builder
WORKDIR /sources

COPY *.csproj .
RUN dotnet restore

COPY . .
RUN dotnet publish --output /app/ --configuration Release

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=builder /app .
CMD ["dotnet", "MyMvc.dll"]

Note how ENTRYPOINT was substituted with CMD — more on that later. So, cd to your app's folder and let's begin.

请注意ENTRYPOINT是如何用CMD替换的-稍后再介绍。 因此,将cd放入您应用的文件夹,开始吧。

  1. Login to Heroku container registry.

    登录到Heroku容器注册表。

    heroku container:login
  2. If you don't have existing git repo, git init a new one

    如果您没有现有的git repo,请git init一个新的

  3. Run heroku create to create a new app, note the git repo address provided, e.g.

    运行heroku create创建一个新应用,注意提供的git repo地址,例如

    Creating salty-fortress-4191... done, stack is heroku-16
    https://salty-fortress-4191.herokuapp.com/ | https://git.heroku.com/salty-fortress-4191.git
  4. (Optional) Check that you have heroku remote by running git remote -v

    (可选)通过运行git remote -v检查您是否拥有heroku remote。

  5. Tell Heroku to use containers:

    告诉Heroku使用容器:

    heroku stack:set container
  6. Create heroku.yml file. Minimalistic version is something like:

    创建heroku.yml文件。 简约版本类似于:

    build:
    docker:
    web: Dockerfile
  7. By default ASP.NET core runs on port 5000 and 5001 (https). Heroku won't allow that. If you try running it as-is, Kestrel won't start, throwing an exception:

    默认情况下,ASP.NET Core在端口5000和5001(https)上运行。 Heroku不允许这样做。 如果尝试按原样运行,则Kestrel无法启动,并引发异常:

    System.Net.Sockets.SocketException (13): Permission denied

    Heroku seems to allow you app to listen on port specified in $PORT environment variable. So you need to ensure your app listens on that, rather than default one. In case you're using the default app, just substitute CreateWebHostBuilder with the following one in Program.cs:

    Heroku似乎允许您的应用程序监听$PORT环境变量中指定的$PORT 。 因此,您需要确保您的应用监听此内容,而不是默认情况。 如果您使用的是默认应用程序,只需用Program.cs的以下内容替换CreateWebHostBuilder

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var port = Environment.GetEnvironmentVariable("PORT");
    
            return WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls("http://*:"+port);
        }
  8. Commit everything:

    提交所有内容:

    git add . && git commit -m 'Meaningful commit message'
  9. Now push the code to get container built and released (fingers crossed):

    现在推送代码以构建并释放容器(手指交叉):

    git push heroku master
    Error

    Now remember when ENTRYPOINT was substituted with CMD in dockerfile? We don't pass any arguments to container, so ENTRYPOINT ["dotnet", "MyMvc.dll"] and CMD ["dotnet", "MyMvc.dll"] should behave similarly. But if you leave ENTRYPOINT, you'll get an error: What a great error — "Unexpected fomation update response status"! Really tells you the root of the problem. The real problem is that when using minimalist heroku.yml that I showed above, Heroku will expect CMD instruction in your dockerfile. When you add it, everything should work just fine.

    现在还记得dockerfile中用CMD替换ENTRYPOINT的时间吗? 我们没有将任何参数传递给容器,因此ENTRYPOINT ["dotnet", "MyMvc.dll"]CMD ["dotnet", "MyMvc.dll"]行为应类似。 但是,如果您离开ENTRYPOINT,则会收到一个错误:多么严重的错误-“意外的更新更新响应状态”! 确实告诉您问题的根源。 真正的问题是,当使用上面显示的最低限度的heroku.yml时,Heroku将在您的dockerfile中使用CMD指令。 当您添加它时,一切都应该正常工作。

结论 (Conclusion)

Now you should have some idea how to deploy simple ASP.NET Core apps to Heroku. Is it intuitive? Absolutely not. Is Heroku the best platform to host your .NET apps? Probably not. But as it's easy to sign up there and the most basic plan is free — maybe you might want to host something there, just for fun.

现在,您应该了解如何将简单的ASP.NET Core应用程序部署到Heroku。 直观吗? 绝对不。 Heroku是托管.NET应用程序的最佳平台吗? 可能不是。 但是,由于在这里注册很容易,而且最基本的计划是免费的-也许您可能想在这里托管一些东西,只是为了好玩。

参考文献 (References)

  1. https://devcenter.heroku.com/articles/container-registry-and-runtime

    https://devcenter.heroku.com/articles/container-registry-and-runtime

  2. https://devcenter.heroku.com/articles/build-docker-images-heroku-yml

    https://devcenter.heroku.com/articles/build-docker-images-heroku-yml

  3. https://docs.docker.com/engine/examples/dotnetcore/ (Dockerfile)

    https://docs.docker.com/engine/examples/dotnetcore/(Dockerfile )

翻译自: https://habr.com/en/post/450904/

heroku

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值