RPM1984
RPM1984

Reputation: 73112

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 noticed this similar question, however it seems outdated and doesn't' have a clear answer.

Can anyone help?

Thanks in advance.

Upvotes: 6

Views: 10128

Answers (2)

RPM1984
RPM1984

Reputation: 73112

Got it working with the help of @peinearydevelopment's answer.

I had to change my docker-compose file to:

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=DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://host.docker.internal:10000/devstoreaccount1;QueueEndpoint=http://host.docker.internal:10001/devstoreaccount1;
    depends_on:
      - azurite
  azurite:
    image: mcr.microsoft.com/azure-storage/azurite
    container_name: aft-backend-azurite
    hostname: azurite
    restart: always
    command: 'azurite --loose --blobHost 0.0.0.0 --blobPort 10000 --queueHost 0.0.0.0 --queuePort 10001 --location /workspace --debug /workspace/debug.log'
    ports:
      - 10000:10000
      - 10001:10001
    volumes:
      - ./azurite:/workspace
    networks:
      - aft-backend

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

The main thing was using the proper Azure Connection String, and making sure the ports matched up to what was being set in the azurite command.

Upvotes: 7

peinearydevelopment
peinearydevelopment

Reputation: 11474

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.

Upvotes: 7

Related Questions