无法从浏览器访问 Docker 应用程序 [英] Unable to access Docker application from browser

查看:63
本文介绍了无法从浏览器访问 Docker 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a .NET Core 2.1 API which, when run via Visual Studio in Windows, receives a HTTP GET from the browser and successfully returns data from a MySQL DB.

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

I've been trying to add the API to a Docker container (inside Ubuntu) - this builds, but I can't access the API from the browser:

我的尝试:

  • 正在检查可用端口.这是相关的输出:

  • 使用其他浏览器连接:在 Chromium 中,它显示无法访问此站点:localhost 意外关闭了连接."
  • 在浏览器中测试未使用的端口,看看这是否会返回不同的消息 - 对于 Firefox,未使用的端口返回无法连接"而不是安全连接失败",而在 Chromium 中,它们返回本地主机拒绝连接".而不是本地主机意外关闭了连接".
  • 在 Firefox 中,将security.tls.insecure_fallback_hosts"配置为localhost".
  • 在 docker-compose.yml 中仔细检查端口是否正确映射 - 在相关容器的端口"下,44329 映射到 44329.
  • 在 API 启动时运行数据库查询并记录结果:这是成功的.我运行 docker-compose up,API 启动,连接到数据库容器,并将 SQL 查询的结果记录到文本文件中.因此,问题不太可能与数据库有关.
  • 记录 GET 请求:在 Windows 中,GET 请求被成功记录,但在 Ubuntu Docker 容器中,它们没有.
  • Connecting using a different browser: In Chromium, it says 'This site can't be reached: localhost unexpectedly closed the connection.'
  • Testing unused ports in the browser to see if this returns a different message - for Firefox, unused ports return 'Unable to connect' instead of 'Secure connection Failed', and in Chromium they return 'localhost refused to connect.' instead of 'localhost unexpectedly closed the connection.'
  • In Firefox, configuring 'security.tls.insecure_fallback_hosts' to 'localhost'.
  • Double-checking that ports are mapped correctly in docker-compose.yml - under 'ports' for the relevant container, 44329 is mapped to 44329.
  • Running a DB query when the API starts and logging the result: this is successful. I run docker-compose up, the API starts, connects to the DB container, and it logs the result of an SQL query to a text file. So the problem is unlikely to be anything to do with the database.
  • Logging GET requests: Inside Windows, the GET requests are successfully logged, but in the Ubuntu Docker container, they're not.

这可能是相关的:在 Windows 中,API 只有在您从 Visual Studio 启动时才能正常工作.如果你执行 dotnet webapi.dll,你会得到这个输出

This might be relevant: In Windows, the API only works properly if you start it from Visual Studio. If you execute dotnet webapi.dll, you get this output

...
etcoreapp2.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.

So here it's not running on 44329; instead it's accessible via 5001 in the browser, where you get this message which is not chill at all: Clicking 'ADVANCED' allows you to continue to the API.

  • 所以我也尝试过在 Ubuntu 中通过 5001 进行连接,但没有成功.

这是docker-compose up"的输出:

Here's the output from '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,我尝试连接到该端口,但没有成功.

    • Seeing the port 80, I've tried connecting to that, without success.

      docker ps显示的相关容器为:

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

      What kind of issue am I looking at here?

      推荐答案

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

      As pointed out by multiple people in the comments (thanks sp0gg, Martin Ullrich and Daniel Lerps), the API was incorrectly listening on port 80.

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

      The solution was to map port 5000 to 44329 in docker-compose.yml for the API container, and also modify the Dockerfile to pass ports in as arguments to dotnet when starting the ASP.NET API:

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

      这篇关于无法从浏览器访问 Docker 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆