4

I little confused with Linux docker and cosmos db emulator. I have an emulator installed on my local machine. On my Windows 10 I have a Linux docker container with Web API ASP.NET core application. When I try to get access from container to cosmos db I get an exception -> HttpRequestException: Connection refused.

In C# code I get needed options like AuthKey and Uri to database from environment variables. Looks like I have an issue with network between container and localhost but I can not understand how I can connect these.

Below provided docker-compose.yml and docker-compose.override.yml files.

event.webapi:
    container_name: event.webapi
    image: '${DOCKER_REGISTRY-}eventwebapi'
    environment:
     **- AzureCollectionName=Events
     - AzureDatabaseName=EventsDatabase**
    build:
      context: .
      dockerfile: src/Services/Event/Event.WebApi/Dockerfile

``` docker-compose.override.yml
event.webapi:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
      - ASPNETCORE_HTTPS_PORT=44378
      **- AzureEndpointUri=https://127.0.0.1:8081
      -AzurePrimaryKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==**
    ports:
      - "53753:80"
      - "44378:443"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
mVadim
  • 43
  • 4
  • This might help https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – Vadim Ashikhman Jul 03 '19 at 00:04

1 Answers1

3

You can't access vm host from docker container inside by setting https://127.0.0.1:8081 directly.

Please refer to this document, and try to set host.docker.internal:8081 to access vm host.

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.


Cosmos DB emulator needs installed SSL certificate,according to this link. For .net runtime, you could access the certificate directly from the Windows Certificate Store.

However, you run the .net code in linux docker image.So my idea is exporting SSL certificate following these steps.

Save it in the specific path on the host and mounting host directories.Please refer to this guide.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • 1
    Thanks a lot for help. Provided information has helped me. But after I changed from https://127.0.0.1:8081 to https://host.docker.internal:8081 I got an exception like this "The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure." – mVadim Jul 03 '19 at 15:40
  • 1
    thanks a lot for provided information. This is really helpful for me. Maybe you know how I can use generated certificate? I have the documentdbemulatorcert.cer file. I shared volume between host and container but I do not know how to use this cert. Could you please help me with this question? – mVadim Jul 04 '19 at 20:57
  • 1
    @mVadim - Any update on this? Going down this path seems the right way, but since the export .cer is for `localhost`, it won't validate against `host.docker.internal`. – DaleyKD May 19 '20 at 21:03