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

查看:340
本文介绍了无法从浏览器访问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稳定时运行数据库查询rts并记录结果:成功。我运行 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中,仅当您从Visual Studio启动API时,API才能正常工作。如果执行 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

...\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进行访问,在此浏览器中您将得到此消息,这一点都不令人毛骨悚然:

单击高级可让您继续

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.

      解决方案是将端口5000映射到docker-compose.yml中的API容器的44329中,并且还可以在启动ASP.NET API时修改Dockerfile,以将端口作为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天全站免登陆