Marin
Marin

Reputation: 193

I can't connect my ASP .NET app from Docker-container to my computer host database with "host.docker.internal:some-port"

I lost amount of time trying to connect my app container with my database Azure Cosmos DB Emulator. I am using loggers object to know where my app break, and I found that the problem is in the connection of the container out of him. I tried to use the famous host.docker.internal direction to connect my host but using my container name (the public IP and DNS internal server of docker).

Here is my appsettings.Development configuration:

  "DocumentDb": {
    "TenantKey": "Default",
    "Endpoint": "https://project-cosmos-container:8081",
    "AuthorizationKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
  },

Here my Dokerfile-Cosmos (here I copy my app .ddl that I created before with dotnet build and dotnet publish):

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"]

And finnally my docker-compose where I run the app:

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

Maybe the problem is in the ports, I don't Know. You can see that I am trying use my port 7000 on my computer host to connect the container and the port 8081 (azure cosmos port)

Upvotes: 1

Views: 1840

Answers (1)

Marin
Marin

Reputation: 193

Unsing the following configuration with host.docker.internal:8081it works.

"DocumentDb": {
    "TenantKey": "Default",
    "Endpoint": "https://host.docker.internal:8081",
    "AuthorizationKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
  },

So using the container name like DNS direction not works. I also have to do in Develpment environment because in other diferent host.docker.internal not works...

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

Upvotes: 0

Related Questions