Search code examples
.netdockerdocker-composeazurite

Connect to Azurite from .NET App via Docker


I have a .NET 6 API (running in Docker), and Azurite (running in Docker).

I'm trying to connect to Azurite from the .NET app using the .NET SDK, but am getting the following error in the Docker logs:

System.AggregateException: Retry failed after 6 tries. Retry settings can be adjusted in ClientOptions.Retry. (Connection refused (azurite:10000)) (Connection refused (azurite:10000)) (Connection refused (azurite:10000)) (Connection refused (azurite:10000)) (Connection refused (azurite:10000)) (Connection refused (azurite:10000))

It's dying on this second line (CreateIfNotExists()):

_blobContainerClient = new BlobContainerClient(connectionString, containerName);
_blobContainerClient.CreateIfNotExists();

Here's my connection string in my .NET app:

"Azure": {
        "StorageConnectionString": "UseDevelopmentStorage=true"
    }

Here is my docker-compose.yml file:

version: '3.4'

services:
  api:
    image: ${DOCKER_REGISTRY-}api
    container_name: aft-backend-api
    build:
      context: src
      dockerfile: API/Dockerfile    
    networks:
      - aft-backend
    environment:
      - ASPNETCORE_URLS=http://+:5000
      - Azure__StorageConnectionString=UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://azurite;
    depends_on:
      - azurite
  azurite:
    image: mcr.microsoft.com/azure-storage/azurite
    container_name: aft-backend-azurite
    hostname: azurite
    restart: always
    command: 'azurite --blobHost 127.0.0.1 --blobPort 10000 --queueHost 127.0.0.1 --queuePort 10001'
    ports:
      - 10000:10000
      - 10001:10001
    networks:
      - aft-backend

networks:
  aft-backend:
    name: aft-backend-network

Things to note:

  • I'm overriding the connection string in the compose file, by using the environment variable
  • I've set the DevelopmentStorageProxyUri to the hostname (azurite)
  • I'm using depends_on to ensure that Azurite starts up before the API

I noticed this similar question, however it seems outdated and doesn't' have a clear answer.

Can anyone help?

Thanks in advance.


Solution

  • I had a hard time with this as well. At the end of the day, this is how I got it working:

    docker-compose.yaml

    version: "3.9"
    services:
      azurite:
        image: mcr.microsoft.com/azure-storage/azurite
        command: "azurite --loose --blobHost 0.0.0.0 --blobPort 10000 --queueHost 0.0.0.0 --queuePort 10001 --location /workspace --debug /workspace/debug.log"
        ports:
          - 10010:10000
          - 10011:10001
          - 10012:10002
        volumes:
          - ./azurite:/workspace
    
      backend:
        build:
          context: backend
          args:
            AzureWebJobsStorage: "${AzureWebJobsStorage}"
        ports:
          - 10004:80
        depends_on:
          - azurite
    

    Connection string used in the app: DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://host.docker.internal:10010/devstoreaccount1;QueueEndpoint=http://host.docker.internal:10011/devstoreaccount1;

    I'm passing in the connection string with and env file, but it should work just as well in your local.settings.json file.