上一节中,我们已经准备好了 SQL Server,那么接下来,我们要把 ASE 放到容器里了。

  首先,新建 Start.ps1,内容如下:

 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 -inporcess
11 }
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 configuration
30 $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 $config
38 
39 # Init the emulator
40 & "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" init -server $SqlServerInstance
41 
42 # Set Initialized flag
43 New-Item Initialized
44 
45 # Start!
46 & "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start -inprocess

 

  Dockerfile:

# escape=`

FROM microsoft/windowsservercore

ADD https://go.microsoft.com/fwlink/?linkid=717179&clcid=0x409 MicrosoftAzureStorageEmulator.msi
RUN msiexec /q /i MicrosoftAzureStorageEmulator.msi
RUN del MicrosoftAzureStorageEmulator.msi
COPY Start.ps1 .

# Azure Storage Emulator
EXPOSE 80

# Configure and launch
ENTRYPOINT powershell .\Start.ps1

 

  或许把 AzureStorageEmulator.exe" init 放到 Dockerfile 里也是个好主意,但是这样每个环境都要 build 不同的 image,这不是我想要的。

  现在可以 Build 我们的 image 了:

docker build -t charmsan/azurestorageemulator .

 

  为了使用 gMSA 运行容器,还有一步(有关容器中使用 gMSA,请见我的另一篇博文:《在 Windows 容器中使用 gMSA》):

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

 

  运行结果:

 

  请无视掉里面的两个错误,配置 Azure Storage Explorer 连接:

 

  运行结果:

 

  现在,我们可以修改 Web.Debug.config 来把连接串 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;" />

 

  不要问我连接串中的 URL 为什么长成这样子,因为你肯定没认真看第一节:)

posted on 2017-10-29 15:17  莱特  阅读(427)  评论(0编辑  收藏  举报