在Elastic Beanstalk上使用Docker部署.NET Core Web API

时间:2022-08-25 09:27:31

I am trying to deploy a bare minimum testing .NET Core Web API using Docker to Elastic Beanstalk without any success.

我正在尝试使用Docker将最初的.NET Core Web API部署到Elastic Beanstalk,但没有成功。


The source code

I've created a brand new .NET Core Web API project in Visual Studio and left the generated example code untouched. After that I added a Dockerfile to the root of the project with the following contents:

我在Visual Studio中创建了一个全新的.NET Core Web API项目,并保持生成的示例代码不变。之后,我将Dockerfile添加到项目的根目录,其中包含以下内容:

FROM microsoft/dotnet:onbuild

EXPOSE 5000

For your curiosity, here's a link to the .NET docker repo.

为了您的好奇心,这里是.NET docker repo的链接。

After that I created a hosting.json file in the root of the project. I wanted to bind the Kestrel server to all IPs of the container. The hosting.json file has the following contents:

之后,我在项目的根目录中创建了一个hosting.json文件。我想将Kestrel服务器绑定到容器的所有IP。 hosting.json文件包含以下内容:

{
    "urls": "http://*:5000"
}

To make sure the app is loading that configuration file I changed my Main method to this:

为了确保应用程序正在加载该配置文件,我将我的Main方法更改为:

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("hosting.json", optional: false)
        .Build();

    var host = new WebHostBuilder()
       .UseKestrel()
       .UseContentRoot(Directory.GetCurrentDirectory())
       .UseIISIntegration()
       .UseStartup<Startup>()
       .UseConfiguration(config)
       .Build();

    host.Run();
}

If needed, here's the documentation on the hosting.json file.

如果需要,这里是hosting.json文件的文档。

Lastly, even though I do not need it according to AWS documentation I created a Dockerrun.aws.json in the root of the project with the following contents:

最后,即使我根据AWS文档不需要它,我在项目的根目录中创建了一个Dockerrun.aws.json,其中包含以下内容:

{
    "AWSEBDockerrunVersion": "1"
}

All this runs just fine on my local machine. I've run it using the following commands:

所有这些在我的本地机器上运行得很好。我使用以下命令运行它:

docker build -t netcore .
docker run --rm -itp 5000:5000 netcore

I've verified that it works by visiting the url http://localhost:5000/api/values in my browser. It produces the expected results!

我已通过在浏览器中访问URL http:// localhost:5000 / api / values来验证它的工作原理。它产生了预期的结果!


AWS

Now in order to deploy it to Elastic Beanstalk I've archived the entire source code togheter with the Dockerfile and Dockerrun.aws.json. The root within the ZIP file looks like this:

现在,为了将它部署到Elastic Beanstalk,我已经使用Dockerfile和Dockerrun.aws.json归档了整个源代码。 ZIP文件中的根看起来像这样:

Controllers/
Properties/
wwwroot/
appsettings.json
Dockerfile
Dockerrun.aws.json
hosting.json
Program.cs
project.json
project.json.lock
Startup.cs
web.config

However, deploying this source bundle to Elastic Beanstalk, using a single Docker container single instance environment, produces the following error No Docker image specified in either Dockerfile or Dockerrun.aws.json. Abort deployment.

但是,使用单个Docker容器单实例环境将此源包部署到Elastic Beanstalk会产生以下错误Dockerfile或Dockerrun.aws.json中指定的无Docker镜像。中止部署。


What am I doing wrong? How do I get this to work?

我究竟做错了什么?我如何让它工作?

1 个解决方案

#1


6  

So this was all developed on a Windows machine, i.e. Windows line endings, and it seems that Elastic Beanstalk does not detect that. That explains why Elastic Beanstalk could not parse a FROM <image> out of my Dockerfile since there's unwanted gibberish, hence the error message No Docker image specified....

所以这一切都是在Windows机器上开发的,即Windows系列结尾,而且似乎Elastic Beanstalk没有检测到这一点。这解释了为什么Elastic Beanstalk无法解析我的Dockerfile中的FROM 在Elastic Beanstalk上使用Docker部署.NET Core Web API,因为有不必要的乱码,因此错误消息No Docker image指定....

I hope this gets noticed and fixed!

我希望这会被注意到并修复!

In the meantime I am using the Visual Studio plugin Line Endings Unifier which enables me to right click on the file and change the line endings to whatever kind I want.

与此同时,我正在使用Visual Studio插件Line Endings Unifier,这使我能够右键单击该文件并将行结尾更改为我想要的任何类型。

#1


6  

So this was all developed on a Windows machine, i.e. Windows line endings, and it seems that Elastic Beanstalk does not detect that. That explains why Elastic Beanstalk could not parse a FROM <image> out of my Dockerfile since there's unwanted gibberish, hence the error message No Docker image specified....

所以这一切都是在Windows机器上开发的,即Windows系列结尾,而且似乎Elastic Beanstalk没有检测到这一点。这解释了为什么Elastic Beanstalk无法解析我的Dockerfile中的FROM 在Elastic Beanstalk上使用Docker部署.NET Core Web API,因为有不必要的乱码,因此错误消息No Docker image指定....

I hope this gets noticed and fixed!

我希望这会被注意到并修复!

In the meantime I am using the Visual Studio plugin Line Endings Unifier which enables me to right click on the file and change the line endings to whatever kind I want.

与此同时,我正在使用Visual Studio插件Line Endings Unifier,这使我能够右键单击该文件并将行结尾更改为我想要的任何类型。