MSSQL SERVER 2016在Windows Docker Container中的安裝

stephenjwq發表於2019-04-19

寫在前面
在如今Docker滿天飛的時代,如果不懂幾招Docker,哪裡說得過去。做為走MS SQL Server 資料庫道路的人,怎麼讓我們的SQL Server和Docker有所交集呢?於是乎,如何讓我們的MSSQL Server跑在炫酷的Docker容器裡呢?這是今天這個帖子的目的。
其次宣告一點的是,這篇文章本來不是我的原創。但是,我想這篇文章的價值在於:

  • 它耗費了我一個周晚上的10:00~12:00時間。謝天謝地,最終成功了,
  • 原創作者文章中有幾處錯誤,請在實驗過程,無比小心。我也是踩了不少坑,
  • 作者很多的文字描述,比較抽象和難於理解,我在適當的地方新增了相關圖片,相信會形象不少


如需要檢視原文,在這裡:
WINDOWS CONTAINERS: INSTALLING SQL SERVER: https://www.pythian.com/blog/installing-sql-server-windows-container/#comment-9311063

場景
是不是厭煩了,一步一步安裝MSSQL SERVER這種超級無趣而又沒有技術含量的體力活?是不是想要定製一個萬能的模版機器,每次僅需要分分鐘搞定SQL SERVER的安裝配置?好了,Docker是你目前最佳選擇了。我們一起讓MSSSQL SERVER 2016炫酷的飛在Docker Container上吧,此為開場白。

知識儲備

  • 簡單的Powershell命令,非常簡單。如果你會Linux,完全可以無縫對接,比如:ls , pwd, mkdird等
  • Windows Azure中虛擬機器的簡單操作,當然不會也無所謂,需要仔細看看截圖
  • 基本的Docker命令:docker ps, docker build, docker stop, docker images, docker commit, docker run等

如果實在沒有接觸過也沒有關係,我們會在相應的地方做適當的註釋。

環境

  • Windows Server 2016 Core with Containers Tech Preview 4: 我在這裡選擇的是Windows Azure虛擬機器。當然你也可以選擇Virtural Box或者VMWare的虛擬機器
  • MSSQL Server 2016 RC1
  • .Net 3.5


安裝步驟

Step 1: Create a New Server
Microsoft Azure => New => Virtual Machines => Windows Server 2016 Core with Containers Tech Preview 4 => click Create button


MSSQL SERVER 2016在Windows Docker Container中的安裝

Basic :configure basic setting

  • hostName: mssql2016host
  • User Name: XXXX
  • XXXXXX

  • MSSQL SERVER 2016在Windows Docker Container中的安裝


Size :chose virtual machine size

MSSQL SERVER 2016在Windows Docker Container中的安裝


  • Settings :configure optional features

        Let's keep all this page the default value


  • Summary :Windows Server 2016 Core with Containers Tech Preview 4

MSSQL SERVER 2016在Windows Docker Container中的安裝

After Initializing Deployment, the Windows Server 2016 for Docker Container will be ready.

MSSQL SERVER 2016在Windows Docker Container中的安裝

Step 2: Configure Azure Security Rules
Your Virtual Machine => Settings => Network interfaces => Network interfaces name => Network security group => Inbound security rules => Add => OK


MSSQL SERVER 2016在Windows Docker Container中的安裝

Step 3: Configure Windows Firewall
Run the following in Powershell on your host to open the right ports. I’ll be using the default port 1433:
if (!(Get-NetFirewallRule | where {$_.Name -eq "SQLServer 1433"})) {
New-NetFirewallRule -Name "SQL Server 1433" -DisplayName "SQL Server 1433" -Protocol tcp -LocalPort 1433 -Action Allow -Enabled True
}


MSSQL SERVER 2016在Windows Docker Container中的安裝

Step 4: Enable .Net 3.5 Framework
get-windowsfeature -name NET-Framework-Features | install-windowsfeature
get-windowsfeature -name NET-Framework-Core | install-windowsfeature


PS C:\Users\cw11> get-windowsfeature -name NET-Framework-Features | install-windowsfeature
Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {.NET Framework 3.5 (includes .NET 2.0 and...


PS C:\Users\cw11> get-windowsfeature -name NET-Framework-Core | install-windowsfeature
Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             NoChangeNeeded {}


Step 5: Download and Extract SQL Server Installation Files
Run the following commands in Powershell on your host to download the SQL Server installers. Change URLs as needed…
##downloading
PS C:\Users\cw11> wget -uri '
SQLServer2016-x64-ENU.exe?tduid=(19e2fef6d95f0cbd6780d6078f12671b)(256380)(2459594)(TnL5HPStwNw-w1OwQOsawKOPA6VG3Jrxrg)(
)' -outfile 'SQLServer2016-x64-ENU.exe'
PS C:\Users\cw11> wget -uri '
SQLServer2016-x64-ENU.box?tduid=(19e2fef6d95f0cbd6780d6078f12671b)(256380)(2459594)(TnL5HPStwNw-z8R0qmxEr41A_GhJe9DScQ)(
)' -outfile 'SQLServer2016-x64-ENU.box'


After you’ve downloaded these two files you can run SQLServer2016-x64-ENU.exe file and choose the extraction path of the SQL files. For the extraction lets make a new folder structure called at C:\MSSQL\Install.
這裡原作者有誤,如果install目錄不為空,會執行失敗,需要做如下處理:
NOTE: before run the .exe file, please make sure C:\mssql\install folder is empty, or else it will encounter an exception. You may have to move those file to another folder, after executing and move them back.


Once you’ve extracted the contents using the .exe you can safely delete the .exe and .box file.


MSSQL SERVER 2016在Windows Docker Container中的安裝


Step 6: Create SQL Server Configuration File & Export registry
## create configurationfile
Copy & parse configurationfile.ini(enclosed following),If you use the same one, make sure you change the password (search for CHANGEME).


##create folder
PS C:\Users\cw11> mkdir -p C:\mssql\install\registrykeys


PS C:\Users\cw11> ls C:\mssql\install\registrykeys
Using regedit, export the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5 registry keys and save them as C:\mssql\install\registrykeys\registry.reg

MSSQL SERVER 2016在Windows Docker Container中的安裝


Step 7: Create your dockerfile
Docker uses the dockerfile as a configuration file and to ensure images are built exactly the same every time.
Take the below code and save it in a text file as c:\mssql\dockerfile


### If you copy parse the dockerfile, please pay attention to the double quotation marks
#Define the base image we'll be building everything else off of...
  
FROM windowsservercore
#Give it a label
LABEL Description="SQL Server" Vendor="Microsoft" Version="13.00.500.53"
#
#These files and folders will be imported into the docker image and be available for us to use.
#
ADD install/ /mssql/install
ADD install/configurationfile.ini /mssql/install/configurationfile.ini
ADD install/registrykeys/registry.reg /mssql/install/registrykeys/registry.reg

MSSQL SERVER 2016在Windows Docker Container中的安裝

Step 8: Build Docker Image
To build the docker image, run command: docker build -t mssql2016 C:\mssql


C:\mssql>docker build -t mssql2016 C:\mssql
Sending build context to Docker daemon 3.188 GB
Step 1 : FROM windowsservercore
...
Step 2 : LABEL Description "SQL Server" Vendor "Microsoft" Version "13.00.500.53"
...
Step 3 : ADD install/ /mssql/install
...
Step 4 : ADD install/configurationfile.ini /mssql/install/configurationfile.ini
...
Step 5 : ADD install/registrykeys/registry.reg /mssql/install/registrykeys/registry.reg
...
Successfully built 94a60dd7a8de


This command tells docker to build an image with a name of mssql2016, and that the dockerfile is located in the c:\mssql folder.
這裡原創作者貼出來的Dockerfile的引號有誤,需要使用英文輸入法的雙引號,否則會報告如下錯誤:
Error:
PS C:\mssql> docker build -t ms sqlserver 2016 c:\mssql
Sending build context to Docker daemon 3.188 GB
Error response from daemon: Syntax error - can't find = in "Server\u0094". Must be of the form: name=value
Solution:
Here this error was caused by dockerfile LABEL double quotation marks issue.
Step 9: Verify
Run the command to verify the built docker image, also you can see another two images.
C:\mssql>docker images
  
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
  
mssql2016           latest              94a60dd7a8de        18 minutes ago      3.187 GB
  
windowsservercore   10.0.10586.0        6801d964fda5        5 months ago        0 B
  
windowsservercore   latest              6801d964fda5        5 months ago        0 B


Step 10: Run your image
Till now, let's run the mssql2016 docker image: docker run -it --name mssqlContainer -p 1433:1433 mssql2016 cmd
  • it | Runs an interactive psuedo-terminal.
  • name | The name of your running container.
  • p 1433:1433 | This bi nds port 1433 on the host to port 1433 on the container process.
In other words, any traffic coming into the host on port 1433 will be forwarded to the container’s port 1433.

  • mssql2016 | The name of the image you want to run.
  • cmd | The utility that you’ll be running.


C:\mssql>docker run -it --name mssqlContainer -p 1433:1433 mssql2016 cmd


Once this image has been running, it will go inside docker image command mode.


MSSQL SERVER 2016在Windows Docker Container中的安裝

Step 11: Enable .Net 3.5 Framework
So, run the following commands to enable .Net 3.5 and import the registry keys.


DISM /online /enable-feature /featurename:NetFx3ServerFeatures
reg import C:\mssql\install\registrykeys\registry.reg


C:\Windows\system32>DISM /online /enable-feature /featurename:NetFx3ServerFeatures
...
The operation completed successfully.

C:\Windows\system32>reg import C:\mssql\install\registrykeys\registry.reg
The operation completed successfully.


Step 12: Install SQL Server in a Windows Container
Finally, it's time to get start SQL SERVER 2016 installing.  Go inside configuration file located folder and run setup command to start.
C:\Windows\System32>cd C:\mssql\install
C:\mssql\install>setup /IAcceptSQLServerLicenseTerms /ConfigurationFile=configurationfile.ini


Once finished, we can see the sqlserver.exe process via task manager.

MSSQL SERVER 2016在Windows Docker Container中的安裝


Step 13: Fix the installation
At this point, the SQL Server instance should be up and running. Unfortunately, there’s a good chance the next time you start the container that the instance will not come up.
The quick fix is to go against every best practice document and run SQL Server under LocalSystem.


sc config MS SQLSERVER obj=LocalSystem


C:\Windows\system32>sc config MSSQLSERVER obj=LocalSystem
[SC] ChangeServiceConfig SUCCESS   



Step 14: Connect to SQL Server
As a test of the instance, you can use OSQL from the command line to verify it’s up and running.
C:\mssql\install>cd "C:\Program Files\Microsoft SQL Server\130\Tools\Binn"
C:\Program Files\Microsoft SQL Server\130\Tools\Binn>OSQL.EXE -E
1> select @@version;
2> go
  
   MicrosoFeb 29 2016 23:19:56 C0) - 13.0.1100.288 (X64)
  
        Entyright (c) Microsoft Corporation
  
        isor)se Evaluation Edition (64-bit) on Windows Server 2016 Technical Preview 4 6.3 <X64> (Build 10586: ) (Hyperv
  
(1 row affected)


Congratulations! You SQL 2016 was built on docker and apply service now.
Step 15: Save your work
At the very last step, you'd better save all your work here. You don't want to lose all your necessary work success, right? OK, let's get start save our work.


  • STOP docker container
C:\mssql>docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
779cd711babc        mssql2016           "cmd"               About an hour ago   Up 7 minutes                            mssqlContainer


C:\mssql>docker stop 779cd711babc
779cd711babc

  • COMMIT the new image to your repository
C:\mssql>docker commit 779cd711babc mssql2016:Installed

  • Try to START the new container again
docker run -it --name mssqlcontainer mssql2016:Installed cmd


此文延伸
延伸問題希望留給大家思考或解答
  • 外部程式或者是SSMS如何理解Docker SQL SERVER Serivce。
  • Docker容器中的SQL SERVER的Storage如何處理,簡單講mdf和ldf檔案怎麼放?Docker的Storage來自哪裡?



轉自:http://www.itpub.net/thread-2056207-1-1.html


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9522838/viewspace-2641882/,如需轉載,請註明出處,否則將追究法律責任。

相關文章