我有一个.NET Core 2.1 API,当通过Windows中的Visual Studio运行时,它会从浏览器接收HTTP GET,并成功地从MySQL数据库返回数据。
我一直在尝试将API添加到Docker容器(在Ubuntu内)-可以构建,但是我无法从浏览器访问API:
我已经试过:
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.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.
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.
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.
Seeing the port 80, I've tried connecting to that, without success.
The relevant container as shown by docker ps
is:
What kind of issue am I looking at here?
正如多人在评论中指出的(感谢sp0gg,Martin Ullrich和Daniel Lerps),该API错误地侦听了端口80。
解决方案是将端口5000映射到docker-compose.yml中用于API容器的端口,并将其映射到API容器的Docker-compose.yml中,将端口5000映射到44329,同时在启动ASP.NET API时将Dockerfile传递为dotnet的参数:
ENTRYPOINT ["dotnet", "webapi.dll", "--urls", "http://*:5000;http://*:5001"]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句