无法从主机机器访问 asp.net core 项目。

huangapple go评论111阅读模式
英文:

I can't access asp.net core project from host machine

问题

Project

docker-compose.yml:

  1. version: "3.9"
  2. services:
  3. aspnetcore_app_service:
  4. build: "./"
  5. ports:
  6. - 5001:5001

Dockerfile:

  1. FROM ubuntu:20.04
  2. RUN apt update
  3. RUN apt install -y wget
  4. RUN wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb
  5. RUN dpkg -i packages-microsoft-prod.deb
  6. RUN apt update
  7. RUN apt install -yq mono-complete aspnetcore-runtime-7.0 dotnet-sdk-7.0
  8. RUN mkdir aspnetcore_app
  9. RUN dotnet new mvc -o aspnetcore_app
  10. WORKDIR aspnetcore_app
  11. CMD [ "dotnet","watch","run","--urls=http://localhost:5001/"]

"docker-compose up" response:

docker-compose up

  1. PS D:\Desktop\Test3\Test3> docker-compose up
  2. [+] Building 0.0s (0/0)
  3. [+] Running 1/1
  4. Container test3-aspnetcore_app_service-1 Created 0.1s
  5. Attaching to test3-aspnetcore_app_service-1
  6. test3-aspnetcore_app_service-1 | dotnet watch 🚀 Started
  7. test3-aspnetcore_app_service-1 | Building...
  8. test3-aspnetcore_app_service-1 | warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
  9. test3-aspnetcore_app_service-1 | No XML encryptor configured. Key {5687eac2-4f72-43f1-b276-d215ea6998ea} may be persisted to storage in unencrypted form.
  10. test3-aspnetcore_app_service-1 | info: Microsoft.Hosting.Lifetime[14]
  11. test3-aspnetcore_app_service-1 | Now listening on: http://localhost:5001
  12. test3-aspnetcore_app_service-1 | dotnet watch 🚀 Unable to launch the browser. Navigate to http://localhost:5001
  13. test3-aspnetcore_app_service-1 | info: Microsoft.Hosting.Lifetime[0]
  14. test3-aspnetcore_app_service-1 | Application started. Press Ctrl+C to shut down.
  15. test3-aspnetcore_app_service-1 | info: Microsoft.Hosting.Lifetime[0]
  16. test3-aspnetcore_app_service-1 | Hosting environment: Development
  17. test3-aspnetcore_app_service-1 | info: Microsoft.Hosting.Lifetime[0]
  18. test3-aspnetcore_app_service-1 | Content root path: /aspnetcore_app
  19. test3-aspnetcore_app_service-1 | warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
  20. test3-aspnetcore_app_service-1 | Failed to determine the https port for redirect.

"curl http://localhost:5001" container response:

container curl response

  1. # curl http://localhost:5001
  2. <!DOCTYPE html>
  3. <html lang="en">

"curl http://localhost:5001 "host machine response:

host machine

  1. C:\Users\Abdullah>curl http://localhost:5001
  2. curl: (52) Empty reply from server

I created an ASP.NET Core project. I want editable, recompileable, live server and these in one container.

英文:

Project

Project

docker-compose.yml:

  1. version: &quot;3.9&quot;
  2. services:
  3. aspnetcore_app_service:
  4. build: &quot;./&quot;
  5. ports:
  6. - 5001:5001

Dockerfile:

  1. FROM ubuntu:20.04
  2. RUN apt update
  3. RUN apt install -y wget
  4. RUN wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb
  5. RUN dpkg -i packages-microsoft-prod.deb
  6. RUN apt update
  7. RUN apt install -yq mono-complete aspnetcore-runtime-7.0 dotnet-sdk-7.0
  8. RUN mkdir aspnetcore_app
  9. RUN dotnet new mvc -o aspnetcore_app
  10. WORKDIR aspnetcore_app
  11. CMD [ &quot;dotnet&quot;,&quot;watch&quot;,&quot;run&quot;,&quot;--urls=http://localhost:5001/&quot;]

"docker-compose up" response:

docker-compose up

  1. PS D:\Desktop\Test3\Test3&gt; docker-compose up
  2. [+] Building 0.0s (0/0)
  3. [+] Running 1/1
  4. Container test3-aspnetcore_app_service-1 Created 0.1s
  5. Attaching to test3-aspnetcore_app_service-1
  6. test3-aspnetcore_app_service-1 | dotnet watch &#128640; Started
  7. test3-aspnetcore_app_service-1 | Building...
  8. test3-aspnetcore_app_service-1 | warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
  9. test3-aspnetcore_app_service-1 | No XML encryptor configured. Key {5687eac2-4f72-43f1-b276-d215ea6998ea} may be persisted to storage in unencrypted form.
  10. test3-aspnetcore_app_service-1 | info: Microsoft.Hosting.Lifetime[14]
  11. test3-aspnetcore_app_service-1 | Now listening on: http://localhost:5001
  12. test3-aspnetcore_app_service-1 | dotnet watch &#127760; Unable to launch the browser. Navigate to http://localhost:5001
  13. test3-aspnetcore_app_service-1 | info: Microsoft.Hosting.Lifetime[0]
  14. test3-aspnetcore_app_service-1 | Application started. Press Ctrl+C to shut down.
  15. test3-aspnetcore_app_service-1 | info: Microsoft.Hosting.Lifetime[0]
  16. test3-aspnetcore_app_service-1 | Hosting environment: Development
  17. test3-aspnetcore_app_service-1 | info: Microsoft.Hosting.Lifetime[0]
  18. test3-aspnetcore_app_service-1 | Content root path: /aspnetcore_app
  19. test3-aspnetcore_app_service-1 | warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
  20. test3-aspnetcore_app_service-1 | Failed to determine the https port for redirect.

"curl http://localhost:5001" container response:

container curl response

  1. # curl http://localhost:5001
  2. &lt;!DOCTYPE html&gt;
  3. &lt;html lang=&quot;en&quot;&gt;

**"curl http://localhost:5001 "host machine response: **
host machine

  1. C:\Users\Abdullah&gt;curl http://localhost:5001
  2. curl: (52) Empty reply from server

I created an ASP.NET Core project. I want editable, recompileable, live server and these in one container.

答案1

得分: 0

Your app binds to localhost, meaning that it'll only accept connections from localhost. In a container, localhost is the container itself. So it rejects your connection attempts, as they're coming from outside the container.

To fix it, make the app bind to 0.0.0.0. Then it'll accept connections from anywhere. To change the binding, change

CMD [ "dotnet","watch","run","--urls=http://localhost:5001/"]

to

CMD [ "dotnet","watch","run","--urls=http://0.0.0.0:5001/"]

英文:

Your app binds to localhost, meaning that it'll only accept connections from localhost. In a container, localhost is the container itself. So it rejects your connection attempts, as they're coming from outside the container.

To fix it, make the app bind to 0.0.0.0. Then it'll accept connections from anywhere. To change the binding, change

  1. CMD [ &quot;dotnet&quot;,&quot;watch&quot;,&quot;run&quot;,&quot;--urls=http://localhost:5001/&quot;]

to

  1. CMD [ &quot;dotnet&quot;,&quot;watch&quot;,&quot;run&quot;,&quot;--urls=http://0.0.0.0:5001/&quot;]

huangapple
  • 本文由 发表于 2023年6月26日 01:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定