英文:
I can't access asp.net core project from host machine
问题
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:
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:
# curl http://localhost:5001
<!DOCTYPE html>
<html lang="en">
"curl http://localhost:5001 "host machine response:
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
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:
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:
# 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.
答案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 [ "dotnet","watch","run","--urls=http://localhost:5001/"]
to
CMD [ "dotnet","watch","run","--urls=http://0.0.0.0:5001/"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论