Olá Pessoal,

Neste post irei mostrar como criar SQL Server rodando dentro de um Windows Container, tudo isso usando docker e powershell.

Primeiro, se você ainda não instalou o docker no seu windows server, assista esse video, para instalar windows server 2016 ou esse para instalar no windows server 2019

Segundo, segue abaixo o dockerfile utilizado com o passo a passo, para criação da nossa imagem.
Neste exemplo nos iremos utilizar o windows server 2019 e o SQL Server 2017 Dev Edition.


FROM mcr.microsoft.com/windows/servercore:ltsc2019

LABEL maintainer "Leandro Scardua"

# Download Links MSSQL 2017 Dev Edition (14.0.1000.169):
ENV exe "https://go.microsoft.com/fwlink/?linkid=840945"
ENV box "https://go.microsoft.com/fwlink/?linkid=840944"

ENV sa_password="_" \
attach_dbs="[]" \
ACCEPT_EULA="_" \
sa_password_path="C:\ProgramData\Docker\secrets\sa-password"

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Copy powershell script
COPY start.ps1 /
WORKDIR /

# Install SQL Server
RUN Invoke-WebRequest -Uri $env:box -OutFile SQL.box ; \
Invoke-WebRequest -Uri $env:exe -OutFile SQL.exe ; \
Start-Process -Wait -FilePath .\SQL.exe -ArgumentList /qs, /x:setup ; \
.\setup\setup.exe /q /ACTION=Install /INSTANCENAME=MSSQLSERVER /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT='NT AUTHORITY\NETWORK SERVICE' /SQLSYSADMINACCOUNTS='BUILTIN\ADMINISTRATORS' /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS ; \
Remove-Item -Recurse -Force SQL.exe, SQL.box, setup
# SQL Configuration
RUN stop-service MSSQLSERVER ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.MSSQLSERVER\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value '' ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.MSSQLSERVER\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.MSSQLSERVER\mssqlserver\' -name LoginMode -value 2 ;

HEALTHCHECK CMD [ "sqlcmd", "-Q", "select 1" ]

CMD .\start -sa_password $env:sa_password -ACCEPT_EULA $env:ACCEPT_EULA -attach_dbs \"$env:attach_dbs\" -Verbose


Agora com o docker instalado e que já vimos como esta estruturado o nosso docker file.
O próximo passo será executar o comandos abaixo, para baixar o dockerfile supracitado, e salvar no caminho C:\ws\sql .
Você pode alterar o caminho do script se necessário.


Invoke-WebRequest https://raw.githubusercontent.com/leandroscardua/Windows-Container/master/dockerfile/mssql/dockerfile-mssql2017Dev-ltsc2019.yml -UseBasicParsing -OutFile (New-Item -Path “C:\ws\sql\dockerfile” -Force)
Invoke-WebRequest https://raw.githubusercontent.com/leandroscardua/Windows-Container/master/dockerfile/mssql/start.ps1 -UseBasicParsing -OutFile (New-Item -Path “C:\ws\sql\start.ps1” -Force)
cd C:\ws\sql


Após a execução do script anterior, nos podemos executar o comando abaixo para criar a imagem.


docker build -t local:sql .


Segue o output do comando abaixo.

PS C:\ws> docker build -t local:mssql .
Sending build context to Docker daemon 6.656kB
Step 1/12 : FROM mcr.microsoft.com/windows/servercore:ltsc2019
—> 987b1d5e0abf
Step 2/12 : LABEL maintainer “Leandro Scardua”
—> Using cache
—> ea6be708bd48
Step 3/12 : ENV exe “https://go.microsoft.com/fwlink/?linkid=840945”
—> Using cache
—> 7962f67f15fb
Step 4/12 : ENV box “https://go.microsoft.com/fwlink/?linkid=840944”
—> Using cache
—> 1fe0aae323fa
Step 5/12 : ENV sa_password=”_” attach_dbs=”[]” ACCEPT_EULA=”_” sa_password_path=”C:\ProgramData\Docker\secrets\sa-password”
—> Using cache
—> 2ecb717084ba
Step 6/12 : SHELL [“powershell”, “-Command”, “$ErrorActionPreference = ‘Stop’; $ProgressPreference = ‘SilentlyContinue’;”]
—> Using cache
—> d039ee095f1a
Step 7/12 : COPY start.ps1 /
—> 121d71d5a2a6
Step 8/12 : WORKDIR /
—> Running in 1bc06cd93091
Removing intermediate container 1bc06cd93091
—> 55f3164985de
Step 9/12 : RUN Invoke-WebRequest -Uri $env:box -OutFile SQL.box ; Invoke-WebRequest -Uri $env:exe -OutFile SQL.exe ; Start-Process -Wait -FilePath .\SQL.exe -ArgumentList /qs, /x:setup ; .\setup\setup.exe /q /ACTION=Install /INSTANCENAME=MSSQLSERVER /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT=’NT AUTHORITY\NETWORK SERVICE’ /SQLSYSADMINACCOUNTS=’BUILTIN\ADMINISTRATORS’ /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS ; Remove-Item -Recurse -Force SQL.exe, SQL.box, setup
—> Running in a4ba7adfdf93
Microsoft .NET Framework CasPol 4.7.3190.0
for Microsoft .NET Framework version 4.7.3190.0
Copyright (C) Microsoft Corporation. All rights reserved.

WARNING: The .NET Framework does not apply CAS policy by default. Any settings
shown or modified by CasPol will only affect applications that opt into using
CAS policy.

Please see http://go.microsoft.com/fwlink/?LinkId=131738 for more information.

Success
Microsoft .NET Framework CasPol 4.7.3190.0
for Microsoft .NET Framework version 4.7.3190.0
Copyright (C) Microsoft Corporation. All rights reserved.

WARNING: The .NET Framework does not apply CAS policy by default. Any settings
shown or modified by CasPol will only affect applications that opt into using
CAS policy.

Please see http://go.microsoft.com/fwlink/?LinkId=131738 for more information.

Success
SQL Server 2017 transmits information about your installation experience, as well as other usage and performance data, to Microsoft to help improve the product. To learn more about SQL Server 2017 data processing and privacy controls, please see the Privacy Statement.
Microsoft (R) SQL Server 2017 14.00.1000.169
Copyright (c) 2017 Microsoft. All rights reserved.

Removing intermediate container a4ba7adfdf93
—> f3e75363eb76
Step 10/12 : RUN stop-service MSSQLSERVER ; set-itemproperty -path ‘HKLM:\software\microsoft\microsoft sql server\mssql14.MSSQLSERVER\mssqlserver\supersocketnetlib\tcp\ipall’ -name tcpdynamicports -value ” ; set-itemproperty -path ‘HKLM:\software\microsoft\microsoft sql server\mssql14.MSSQLSERVER\mssqlserver\supersocketnetlib\tcp\ipall’ -name tcpport -value 1433 ; set-itemproperty -path ‘HKLM:\software\microsoft\microsoft sql server\mssql14.MSSQLSERVER\mssqlserver\’ -name LoginMode -value 2 ;
—> Running in 82f9d73dff76
WARNING: Waiting for service ‘SQL Server (MSSQLSERVER) (MSSQLSERVER)’ to
stop…
WARNING: Waiting for service ‘SQL Server (MSSQLSERVER) (MSSQLSERVER)’ to
stop…
WARNING: Waiting for service ‘SQL Server (MSSQLSERVER) (MSSQLSERVER)’ to
stop…
WARNING: Waiting for service ‘SQL Server (MSSQLSERVER) (MSSQLSERVER)’ to
stop…
WARNING: Waiting for service ‘SQL Server (MSSQLSERVER) (MSSQLSERVER)’ to
stop…
Removing intermediate container 82f9d73dff76
—> 5e23770c2736
Step 11/12 : HEALTHCHECK CMD [ “sqlcmd”, “-Q”, “select 1” ]
—> Running in f6f1daa13509
Removing intermediate container f6f1daa13509
—> 3ce914011b15
Step 12/12 : CMD .\start -sa_password $env:sa_password -ACCEPT_EULA $env:ACCEPT_EULA -attach_dbs \”$env:attach_dbs\” -Verbose
—> Running in b7fa30c0b080
Removing intermediate container b7fa30c0b080
—> 124a9cbbc02e
Successfully built 124a9cbbc02e
Successfully tagged local:mssql

Valeu pessoal, até a próxima.

Deixe uma resposta