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

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

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

问题

Project

docker-compose.yml:

version: "3.9"
services:
  aspnetcore_app_service:
    build: "./"
    ports:
      - 5001:5001

Dockerfile:

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

"docker-compose up" response:

docker-compose up

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

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

container curl response

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

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

host machine

C:\Users\Abdullah>curl http://localhost:5001
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:

version: &quot;3.9&quot;
services:
  aspnetcore_app_service:
    build: &quot;./&quot;
    ports:
      - 5001:5001

Dockerfile:

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

"docker-compose up" response:

docker-compose up

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

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

container curl response

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

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

C:\Users\Abdullah&gt;curl http://localhost:5001
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

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

to

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:

确定