Run Azure Storage Emulator in Windows Server Container (3): Run in Container, azureemulator
In the previous section, we have prepared SQL Server, so next we will put the ASE in the container.
First, create Start. ps1 with the following content:
1 param( 2 [Parameter(Mandatory=$true)][string]$HostName, 3 [string]$AccountName, 4 [string]$AuthKey, 5 [string]$SqlServerInstance) 6 7 # Initialized? 8 if (Test-Path Initialized) 9 {10 &"C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start -inporcess11 }12 13 if ([string]::IsNullOrEmpty($AccountName))14 {15 Write-Output "AccountName argument not specified, use the default: devstoreaccount1"16 $AccountName = "devstoreaccount1"17 }18 if ([string]::IsNullOrEmpty($AuthKey))19 {20 Write-Output "AuthKey argument not specified, use the default: Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="21 $AuthKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="22 }23 if ([string]::IsNullOrEmpty($SqlServerInstance))24 {25 Write-Output "SqlServerInstance argument not specified, use the default: (localdb)\MSSQLLocalDB"26 $SqlServerInstance = "(localdb)\MSSQLLocalDB"27 }28 29 # Replace the configuration30 $config = "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe.config"31 (get-content $config)`32 -Replace "<service name=""Blob"" url=""http://127.0.0.1:10000/""/>", "<service name=""Blob"" url=""http://$AccountName.blob.$HostName/"" />"`33 -Replace "<service name=""Queue"" url=""http://127.0.0.1:10001/""/>", "<service name=""Queue"" url=""http://$AccountName.queue.$HostName/"" />"`34 -Replace"<service name=""Table"" url=""http://127.0.0.1:10002/""/>", "<service name=""Table"" url=""http://$AccountName.table.$HostName/"" />"`35 -Replace "<account name=""devstoreaccount1"" authKey=""Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="" />",`36 "<account name=""$AccountName"" authKey=""$AuthKey"" />"`37 | Out-File $config38 39 # Init the emulator40 & "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" init -server $SqlServerInstance41 42 # Set Initialized flag43 New-Item Initialized44 45 # Start!46 & "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start -inprocess
Dockerfile:
# escape=`FROM microsoft/windowsservercoreADD https://go.microsoft.com/fwlink/?linkid=717179&clcid=0x409 MicrosoftAzureStorageEmulator.msiRUN msiexec /q /i MicrosoftAzureStorageEmulator.msiRUN del MicrosoftAzureStorageEmulator.msiCOPY Start.ps1 .# Azure Storage EmulatorEXPOSE 80# Configure and launchENTRYPOINT powershell .\Start.ps1
It may be a good idea to put AzureStorageEmulator.exe "init in Dockerfile, but it is not what I want to build different images in every environment.
Now we can Build our image:
docker build -t charmsan/azurestorageemulator .
To use gMSA to run containers, there is another step (for details about how to use gMSA in containers, please refer to my another blog post: Use gMSA in Windows containers):
New-CredentialSpec -Name AseSvc -AccountName AseSvc
Launch!
docker run -it -h ASE-DEV --name AzureStorageEmulator-Dev --network external --ip 192.168.11.1 --dns 192.168.15.1 --dns 192.168.15.2 --security-opt "credentialspec=file://AseSvc.json" --restart unless-stopped charmsan/azurestorageemulator -HostName contoso.com -AccountName dev -SqlServerInstance CONTOSO-ENV-DB\CONTOSO_DEV
Running result:
Ignore the two errors and configure the Azure Storage Explorer connection:
Running result:
Now, we can modify Web. Debug. config to set the connection string to transform:
<add xdt:Transform="SetAttributes" xdt:Locator="Match(key)" key="StorageAccountConnectionString" value="DefaultEndpointsProtocol=http;AccountName=dev;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://dev.blob.contoso.com/dev;" />
Don't ask me why the URL in the connection string looks like this, because you certainly didn't take the first section seriously :)