MSSQL SERVER 2016在Windows Docker Container中的安装

写在前面
在如今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


Basic :configure basic setting

  • hostName: mssql2016host
  • User Name: XXXX
  • XXXXXX


Size :chose virtual machine size


  • Settings :configure optional features

        Let's keep all this page the default value


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

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

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


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
}


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 ' http://care.dlservice.microsoft.com/dl/download/7/6/0/760057FC-6A60-4593-B2A2-2CA7CE0BFEA2/
SQLServer2016-x64-ENU.exe?tduid=(19e2fef6d95f0cbd6780d6078f12671b)(256380)(2459594)(TnL5HPStwNw-w1OwQOsawKOPA6VG3Jrxrg)(
)' -outfile 'SQLServer2016-x64-ENU.exe'
PS C:\Users\cw11> wget -uri ' http://care.dlservice.microsoft.com/dl/download/7/6/0/760057FC-6A60-4593-B2A2-2CA7CE0BFEA2/
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.



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


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

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.


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.


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 (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


请使用浏览器的分享功能分享到微信等