尝试将我的应用程序容器与我的数据库 Azure Cosmos DB Emulator 连接时,我浪费了大量时间。我正在使用 loggers 对象来了解我的应用程序中断的位置,我发现问题出在容器与他之间的连接上。我尝试使用著名的 host.docker.internal 方向连接我的主机,但使用我的容器名称(docker 的公共(public) IP 和 DNS 内部服务器)。
这是我的 appsettings.Development 配置:
"DocumentDb": {
"TenantKey": "Default",
"Endpoint": "https://project-cosmos-container:8081",
"AuthorizationKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
},
这里是我的 Dokerfile-Cosmos(这里我复制了我之前使用 dotnet build 和 dotnet publish 创建的应用程序 .ddl):
FROM microsoft/dotnet:2.2-aspnetcore-runtime
# We create the folder inside the container
WORKDIR /local-project
# We are coping all project executables that we created with dotnet build and dotnet publish
COPY ./bin/Release/netcoreapp2.2/publish/* ./
EXPOSE 8000
EXPOSE 8081
# We indicate to execute the program in the executable of the project
ENTRYPOINT ["dotnet", "Local.Proyect.Core.dll"]
最后是我运行应用程序的 docker-compose:
version: '3.1'
services:
local-Proyect:
image: project-cosmos-image
container_name: project-cosmos-container
ports:
- 127.0.0.1:7000:8000
environment:
ASPNETCORE_ENVIRONMENT: Development
ASPNETCORE_URLS: http://+:8000
也许问题出在端口上,我不知道。您可以看到我正在尝试使用我的计算机主机上的端口 7000 来连接容器和端口 8081 ( azure cosmos 端口 )
最佳答案
使用 host.docker.internal:8081
取消以下配置有用。
"DocumentDb": {
"TenantKey": "Default",
"Endpoint": "https://host.docker.internal:8081",
"AuthorizationKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
},
所以使用像 DNS 方向这样的容器名称是行不通的。我也必须在开发环境中做,因为在其他不同的
host.docker.internal
不工作...version: '3.1'
services:
local-Proyect:
image: project-cosmos-image
container_name: project-cosmos-container
ports:
- 127.0.0.1:7000:433
- 127.0.0.1:7001:80
environment:
ASPNETCORE_ENVIRONMENT: Development
ASPNETCORE_URLS: http://+:433;http://+:80
关于asp.net - 我无法使用 "host.docker.internal:some-port"将我的 ASP .NET 应用程序从 Docker 容器连接到我的计算机主机数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59244593/