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#](https://i-blog.csdnimg.cn/blog_migrate/fd11ed7ba52582a3edb89f46eb830f20.png)
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放入您应用的文件夹,开始吧。
Login to Heroku container registry.
登录到Heroku容器注册表。
heroku container:login
If you don't have existing git repo,
git init
a new one如果您没有现有的git repo,请
git init
一个新的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
(Optional) Check that you have heroku remote by running
git remote -v
(可选)通过运行
git remote -v
检查您是否拥有heroku remote。Tell Heroku to use containers:
告诉Heroku使用容器:
heroku stack:set container
Create heroku.yml file. Minimalistic version is something like:
创建heroku.yml文件。 简约版本类似于:
build: docker: web: Dockerfile
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 substituteCreateWebHostBuilder
with the following one inProgram.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); }
Commit everything:
提交所有内容:
git add . && git commit -m 'Meaningful commit message'
Now push the code to get container built and released (fingers crossed):
现在推送代码以构建并释放容器(手指交叉):
git push heroku master
Now remember when ENTRYPOINT was substituted with CMD in dockerfile? We don't pass any arguments to container, so
ENTRYPOINT ["dotnet", "MyMvc.dll"]
andCMD ["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 minimalistheroku.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)
https://devcenter.heroku.com/articles/container-registry-and-runtime
https://devcenter.heroku.com/articles/container-registry-and-runtime
https://devcenter.heroku.com/articles/build-docker-images-heroku-yml
https://devcenter.heroku.com/articles/build-docker-images-heroku-yml
https://docs.docker.com/engine/examples/dotnetcore/ (Dockerfile)
https://docs.docker.com/engine/examples/dotnetcore/(Dockerfile )
heroku