简体   繁体   中英

Customized storage account for Azurite on docker compose

I am trying to use Azurite for my testing environment, I added it to the docker compose file, and it worked fine, code below:

azurite:
image: mcr.microsoft.com/azure-storage/azurite
container_name: test-azurite
ports:
  - '10001:10001'
  - '10000:10000'

I need to use a custom storage account name other than the default one "devstoreaccount1", so I updated the account name on the different connection strings, and I added the account name as environment variable for the azurite container, now it looks like below:

azurite:
image: mcr.microsoft.com/azure-storage/azurite
container_name: test-azurite
ports:
  - '10001:10001'
  - '10000:10000'
environment:
  - AZURITE_ACCOUNTS="newstorageaccount:newkey"

I tried also adding it as command:

commands:
- export AZURITE_ACCOUNTS="newstorageaccount:newkey"

But that didn't work, any help on how to get Azurite to work with a customized storage account on docker compose is appreciated, Thanks

Works for me like this, even if I don't use a valid base64 encoded string as a key:

version: '2'
services:
  azurite:
    image: mcr.microsoft.com/azure-storage/azurite:3.19.0
    container_name: test-azurite
    ports:
      - '10001:10001'
      - '10000:10000'
    environment:
      - AZURITE_ACCOUNTS=myaccountname:ZGV2c3RvcmVhY2NvdW50Mw==
var accountName = "myaccountname";
var key = "ZGV2c3RvcmVhY2NvdW50Mw==";
var connectionString = $"DefaultEndpointsProtocol=http;AccountName={accountName};AccountKey={key};BlobEndpoint=http://127.0.0.1:10000/{accountName};";

BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
await blobServiceClient.GetBlobContainerClient("test2").CreateIfNotExistsAsync();

The docker-compose file provided by @AlexAIT in his answer will probably work.

Having said that, just to shed some light on the issue, the problem could be motivated by several reasons.

On one hand, the Azurite Github repository itself provides an issue which largely resembles your problem.

It provides as well a solution in the comment provided by blueww consisting in removing the doubles quotes when defining the variable:

It looks in docker-compose.yaml , you should set the environment variable as following: (note " are removed)

 environment: - AZURITE_ACCOUNTS=newstorageaccount:newkey

Please, consider read the official docker documentation for further alternatives like env ironment files for providing environment variables information.

Another possible reason of the problem could be the fact that you may be providing an account key that it is not encoded in base 64. I am aware that Alex mentioned in his answer that Azurite work even in that case but at least certain tools like Microsoft Azure Storage Explorer will be unable to connect to the emulator unless you provide a properly base 64 encoded key.

The requirement of the base 64 key is indicated several times in the Microsoft documentation and as you can see in the source code of the library is the format expected when parsing the provided environment variable:

private parserAccountsEnvironmentString(accounts: string): IAccounts {
  // account1:key1
  // account1:key1:key2
  // account1:key1:key2;account2:key2;
  const results: IAccounts = {};
  const accountsArray = accounts.trim().split(";");
  accountsArray.forEach(accountAndKeys => {
    if (accountAndKeys.length > 0) {
      const parts = accountAndKeys.split(":");
      if (parts.length < 2 || parts.length > 3) {
        throw RangeError(
          `AccountDataStore:parserAccountsEnvironmentString() Invalid environment string format for ${accounts}`
        );
      }
      const account = parts[0];
      const key1 = parts[1];
      const key2 = parts.length > 2 ? parts[2] : undefined;
      results[account] = {
        name: account,
        key1: Buffer.from(key1, "base64"),
        key2: key2 ? Buffer.from(key2, "base64") : undefined
      };
    }
  });


  return results;
}

Note the line:

key1: Buffer.from(key1, "base64")

This is an example of a working docker-compose.yml file, mostly identical to the one provided by Alex in his answer, with the difference of overriding the image command for providing debug information:

version: '3.8'
services:

  azurite:
    image: mcr.microsoft.com/azure-storage/azurite
    container_name: test-azurite
    ports:
      - "10101:10001"
      - "10000:10000"
    command: ["azurite", "-l", "/data", "--blobHost", "0.0.0.0","--queueHost", $
    environment:
      - AZURITE_ACCOUNTS=mystorageaccount:cjAJ67kitgcwiq25u44V4x9xHgk6vFN1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM