Search code examples
dockerdocker-composeazurite

Azure Blob Storage error in Django - Failed to establish a new connection: [Errno 111]


I am setting up a local Azure Blob Storage using Docker container & Docker-compose. However, when I start creating blob containers and uploading files it throws me the error as below.

azure.common.AzureException: HTTPConnectionPool(host='127.0.0.1', port=10000): Max retries exceeded with url: /devstoreaccount1/quickstartblobs?restype=container (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f1068d0f748>: Failed to establish a new connection: [Errno 111] Connection refused',))

Here is my docker-compose:

version: "3.9"
   
services:
 
      
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - DEBUG=FALSE
      - AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=https;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=https://127.0.0.1:10000/devstoreaccount1;
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
      - 5678:5678
    depends_on:
      - db
      
  azurite:
    image: mcr.microsoft.com/azure-storage/azurite
    ports:
      - "127.0.0.1:10000:10000"

Requirements.txt

djangorestframework==3.11.2
Django==3.1.8
Pygments==2.7.4
Markdown==3.2.1
coreapi==2.3.3
psycopg2-binary==2.8.4
dj-database-url==0.5.0
gunicorn==20.0.4
whitenoise==5.0.1
PyYAML==5.4
azure-storage-blob==2.1.0
ptvsd==4.3.2
azure-common==1.1.23
azure-storage-common==2.1.0
requests==2.25.1
six==1.11.0
urllib3==1.26.3

Code:

 blob_service_client = BlockBlobService(
            account_name='devstoreaccount1', account_key='Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==',is_emulated=True)

        # Create a container called 'quickstartblobs'.
        container_name = 'quickstartblobs'
        blob_service_client.create_container(container_name)

Solution

  • You can remove the ports section for azurite service in your compose file and in your application provide the connection string and specify the blob endpoint (as mentioned here: https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azurite#connection-strings) as BlobEndpoint=http://azurite:10000

    When you use docker local bridge (created for services where deployed using compose), container name if provided explicitly else the service name can be used to access the service.