无法从浏览器访问Docker应用程序

无法从浏览器访问Docker应用程序,docker,asp.net-core,.net-core,docker-compose,ubuntu-16.04,Docker,Asp.net Core,.net Core,Docker Compose,Ubuntu 16.04,我有一个.NET Core 2.1 API,在Windows中通过Visual Studio运行时,它从浏览器接收HTTP GET,并成功地从MySQL数据库返回数据 我一直在尝试将API添加到Docker容器(在Ubuntu内部)-这是一个构建,但我无法从浏览器访问API: 我所尝试的: 正在检查可用端口。以下是相关的输出: 使用其他浏览器连接:在Chromium中,它表示“无法访问此站点:localhost意外关闭了连接。” 在浏览器中测试未使用的端口以查看是否返回不同的消息-对于

我有一个.NET Core 2.1 API,在Windows中通过Visual Studio运行时,它从浏览器接收HTTP GET,并成功地从MySQL数据库返回数据

我一直在尝试将API添加到Docker容器(在Ubuntu内部)-这是一个构建,但我无法从浏览器访问API:

我所尝试的

  • 正在检查可用端口。以下是相关的输出:

  • 使用其他浏览器连接:在Chromium中,它表示“无法访问此站点:localhost意外关闭了连接。”
  • 在浏览器中测试未使用的端口以查看是否返回不同的消息-对于Firefox,未使用的端口返回“无法连接”而不是“安全连接失败”,在Chromium中,它们返回“localhost拒绝连接”。而不是“localhost意外关闭连接”
  • 在Firefox中,将“security.tls.unsecurity\u fallback\u hosts”配置为“localhost”
  • 仔细检查docker-compose.yml中的端口映射是否正确-在相关容器的“端口”下,44329映射到44329
  • 在API启动时运行DB查询并记录结果:这是成功的。我运行
    docker compose up
    ,API启动,连接到DB容器,并将SQL查询的结果记录到文本文件中。因此,问题不太可能与数据库有关
  • 记录GET请求:在Windows中,GET请求被成功记录,但在Ubuntu Docker容器中,它们没有
这可能与此相关:在Windows中,只有从VisualStudio启动API,它才能正常工作。如果执行
dotnetwebapi.dll
,则会得到此输出

...\netcoreapp2.1>dotnet webapi.dll
...
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
所以这里它不是在44329上运行;相反,它可以通过浏览器中的5001访问,在浏览器中您可以看到一条根本不冷的消息: 单击“高级”可继续访问API

  • 所以我也尝试过在Ubuntu中通过5001进行连接,但运气不好
以下是“docker compose up”的输出:

matt@Matt-Ubuntu:~/docker2$ docker-compose up
Starting docker2_mysql_1 ... done
Starting docker2_dbmodelmapper_1 ... done
Attaching to docker2_mysql_1, docker2_dbmodelmapper_1
... (mysql stuff) ...
dbmodelmapper_1  | Hosting environment: Production
dbmodelmapper_1  | Content root path: /app
dbmodelmapper_1  | Now listening on: http://[::]:80
dbmodelmapper_1  | Application started. Press Ctrl+C to shut down.
  • 看到端口80,我尝试连接到它,但没有成功

  • docker ps
    所示的相关容器为:


我在这里看到的是什么样的问题?

正如多人在评论中指出的那样(感谢sp0gg、Martin Ullrich和Daniel Lerps),API在端口80上侦听错误

解决方案是将API容器的docker-compose.yml中的端口5000映射到44329,并修改Dockerfile,以便在启动ASP.NET API时将端口作为参数传递到dotnet:

ENTRYPOINT ["dotnet", "webapi.dll", "--urls", "http://*:5000;http://*:5001"]

docker ps
的输出是什么?在问题的末尾添加了这可能是防火墙问题吗?似乎不是-ufw被禁用,docker配置了iptables以允许这些端口。当您尝试连接到端口80时,您是否发布了端口?上面的输出没有显示端口80。