天天看點

Windows Server 2016上具有Docker容器SQL Server

In this article let’s see how to install a SQL Server Instance using a Docker container in Windows server 2016.

在本文中,讓我們看看如何在Windows Server 2016中使用Docker容器安裝SQL Server執行個體。

“Containers” is probably one of the hottest buzz words talked about these days in IT world, especially Docker containers. So, what exactly is Docker? Well, Docker is one of the few companies who develops, builds and distributes container technologies. It’s a pioneer in container technologies, similar to VMWare in virtualization technologies. I won’t be going into internals and architecture of how docker container works under the hoods in this article, instead lets just focus on installing docker containers inside windows server 2016 and then setup the latest version of SQL Server Instance on top of it.

“容器”可能是當今IT世界中最熱門的流行語之一,尤其是Docker容器。 那麼,Docker到底是什麼? 嗯,Docker是開發,建構和分發容器技術的少數公司之一。 它是容器技術的先驅,類似于VMWare的虛拟化技術。 我不會在本文中深入探讨Docker容器如何工作的内部結構和體系結構,而是隻專注于在Windows Server 2016中安裝Docker容器,然後在其之上設定最新版本SQL Server執行個體。

One can install dockers in windows 10 (should be enterprise or professional with anniversary update or later and Hyper-V enabled.) or in Windows server 2016, In our case we will be using Windows Server 2016 for this purpose.

一個人可以在Windows 10(應該是具有周年紀念日更新或更高版本并啟用了Hyper-V的企業版或專業版)中或在Windows Server 2016中安裝docker,在本例中,我們将為此目的使用Windows Server 2016。

在Windows Server 2016上安裝Docker服務 (Installing Docker services on windows server 2016)

In order to install Docker suite, first we have to enable containers feature in windows server 2016 and install Microsoft management package provider for docker. Okay, let’s get into action. To begin with, I created a Windows server 2016 Virtual machine and enabled direct internet access to the VM.

為了安裝Docker套件,首先我們必須在Windows Server 2016中啟用容器功能并為Docker安裝Microsoft管理軟體包提供程式。 好吧,讓我們開始行動。 首先,我建立了Windows Server 2016虛拟機并啟用了對VM的直接Internet通路。

Open server manager and navigate to “Add roles and features” and select “Containers” feature as shown below.

打開伺服器管理器 ,然後導航到“ 添加角色和功能 ”,然後選擇“ 容器 ”功能,如下所示。

Windows Server 2016上具有Docker容器SQL Server
Windows Server 2016上具有Docker容器SQL Server
Windows Server 2016上具有Docker容器SQL Server

Select “Containers” feature and click “Next”.

選擇“ 容器”功能,然後單擊“ 下一步 ”。

Windows Server 2016上具有Docker容器SQL Server

After a minute or so, the server got rebooted as per the option selected above. Now let’s download and install Microsoft package management provider for docker. For that. Open PowerShell in elevated mode (Run as an administrator) and issue below command.

大約一分鐘後,伺服器根據上面選擇的選項重新啟動。 現在,讓我們下載下傳并安裝适用于docker的Microsoft軟體包管理提供程式。 為了那個原因。 以提升模式打開PowerShell( 以管理者身份運作 ),然後發出以下指令。

Install-Module -Name DockerMsftProvider -Force
           
Windows Server 2016上具有Docker容器SQL Server

You will be prompted to download and install NuGet provider as well as part of this installation, Type “Y” or just hit enter to proceed further.

系統将提示您下載下傳并安裝NuGet provider以及此安裝的一部分,鍵入“ Y ”或直接按Enter即可繼續。

Windows Server 2016上具有Docker容器SQL Server

Now it’s time to install the Docker package. Issue the below provided PowerShell command to install the latest version of Docker. (This will install both Docker engine and the Docker client on your machine)

現在是時候安裝Docker軟體包了。 發出以下提供的PowerShell指令以安裝最新版本的Docker。 (這将在您的計算機上安裝Docker引擎和Docker用戶端)

Install-Package -Name docker -ProviderName DockerMsftProvider -Force
           
  • Note: If you have the latest updates installed on your windows server 2016, you should not run into any issues.注意:如果您在Windows Server 2016上安裝了最新更新,則不應遇到任何問題。

In my case, I didn’t have the prerequisite patches installed on my server, so my installation ran into issues as shown in the below screenshot.

就我而言,我沒有在伺服器上安裝必備更新檔程式,是以我的安裝遇到了以下螢幕截圖所示的問題。

Windows Server 2016上具有Docker容器SQL Server

Well, let’s apply the latest and greatest updates available for my windows server 2016 invoking “sconfig”.

好吧,讓我們應用Windows Server 2016調用“ sconfig ”的最新和最大更新。

Windows Server 2016上具有Docker容器SQL Server

After few seconds, you should get below screen, from which you can select “Option 6” to download and install latest updates.

幾秒鐘後,您将進入下面的螢幕,從中可以選擇“ Option 6 ”以下載下傳并安裝最新更新。

Windows Server 2016上具有Docker容器SQL Server

At this point, it prompted me to select either to install “All updates” or “Only recommended updates”. I opted for installing all updates as you can see in the below screenshots.

此時,它提示我選擇安裝“ 所有更新 ”或“ 僅推薦更新 ”。 如下面的螢幕截圖所示,我選擇安裝所有更新。

Windows Server 2016上具有Docker容器SQL Server
Windows Server 2016上具有Docker容器SQL Server
Windows Server 2016上具有Docker容器SQL Server

As you can see, after few minutes I got a popup requesting for system reboot. Select “Yes” and once the server is back online, I tried issuing the same PowerShell command which failed earlier to Install Docker services on my machine.

如您所見,幾分鐘後,我彈出一個視窗,要求系統重新啟動。 選擇“ 是 ”,并且伺服器重新聯機後,我嘗試發出相同的PowerShell指令,該指令先前未能在我的計算機上安裝Docker服務。

Okay, Let’s retry again…

好吧,讓我們再試一次...

Install-Package -Name docker -ProviderName DockerMsftProvider -Force
           
Windows Server 2016上具有Docker容器SQL Server

Awesome, this time I had no issues. At this point, you should be able to see Docker being installed as a service under “Services.msc” in stopped state. You can start the service from services console or issue below PowerShell statement to start the service.

太棒了,這次我沒有問題。 此時,您應該能夠看到Docker作為服務安裝在“ Services.msc ”下,處于停止狀态。 您可以從服務控制台啟動服務,也可以在PowerShell語句下面發出指令以啟動服務。

get-service | where-object{$_.name -eq "docker"} | start-service
           
Windows Server 2016上具有Docker容器SQL Server

Great, now that we have Docker installed on our server, it is time to verify few basic things. Issue “Docker Info” command to get information related to docker installation we just finished.

太好了,既然我們已經在伺服器上安裝了Docker,是時候驗證一些基本内容了。 發出“ Docker Info ”指令以擷取與我們剛剛完成的Docker安裝相關的資訊。

Windows Server 2016上具有Docker容器SQL Server

Issue “Docker version” to get version details as shown below.

發出“ Docker版本”以擷取版本詳細資訊,如下所示。

Windows Server 2016上具有Docker容器SQL Server

If you are curious about all the available commands we can use with Docker, just type “docker” and hit enter. It will provide us with a long list of all available commands as shown below.

如果您對我們可用于Docker的所有可用指令感到好奇,隻需鍵入“ docker ”并按Enter 。 它将為我們提供所有可用指令的長長清單,如下所示。

Windows Server 2016上具有Docker容器SQL Server

在Windows容器上安裝/運作SQL Server (Installing/running SQL Server on Windows containers)

Now that we have Docker service up and running on our server, we should be able to create SQL containers. First thing first, in order to create a SQL container, we need to have an image. Think of an image as our SQL Server installation media. Similar to how we need media to create a SQL Instance on a machine, we need an image to run a SQL container. These images can be stored and pulled (Analogy – download in our traditional setup) from your own private registry if you have one setup or from Docker hub. Docker hub is a registry/repository provided by Docker company where anyone can upload and store their images. For this article, I will be using an image published by Microsoft in docker hub to create my SQL containers.

現在我們已經在伺服器上啟動并運作了Docker服務,我們應該能夠建立SQL容器。 首先,為了建立一個SQL容器,我們需要有一個映像。 将映像視為我們SQL Server安裝媒體。 與我們需要媒體在計算機上建立SQL執行個體的方式類似,我們需要一個映像來運作SQL容器。 如果您有一個設定或從Docker集線器,則可以從您自己的私有系統資料庫中存儲和拉取這些映像(Analog –在我們的傳統設定中下載下傳)。 Docker Hub是Docker公司提供的系統資料庫/存儲庫,任何人都可以上傳和存儲其映像。 對于本文,我将使用Microsoft在docker hub中釋出的圖像來建立我SQL容器。

To begin with, Let’s search all the Microsoft SQL images available in docker hub by issuing below command.

首先,讓我們通過發出以下指令來搜尋docker hub中可用的所有Microsoft SQL映像。

docker search microsoft | select-string sql
           
Windows Server 2016上具有Docker容器SQL Server

Just for fun, let’s see if I have any images already available in my local repository. I can issue the command “docker images” for that purpose. As you can see, I have none at this point.

隻是為了好玩,讓我們看看本地存儲庫中是否有可用的圖像。 為此,我可以發出指令“ docker images ”。 如您所見,目前我還沒有。

Windows Server 2016上具有Docker容器SQL Server

Now, let’s pull (Again…think of it like downloading the media) “mssql-server-windows-developer” to my local registry from docker hub by issuing the below command

現在,讓我們通過發出以下指令從Docker Hub将(再次…… 想起來就像下載下傳媒體一樣)“ mssql-server-windows-developer ”到我的本地系統資料庫中

“docker pull Microsoft/mssql-server-windows-developer”.
           
Windows Server 2016上具有Docker容器SQL Server

Depending on your download speeds, it might take a while. Once it’s done pulling the image from hub, I am issuing the “docker images” command again to check if things appear any different.

根據您的下載下傳速度,可能需要一段時間。 完成從集線器中提取映像後,我将再次發出“ docker images ”指令以檢查情況是否有所不同。

Windows Server 2016上具有Docker容器SQL Server

As you can see now, I have an image downloaded to my local repository which is ~15 GB in size. You can also notice that this image has been published/created by Microsoft in docker hub 14 months ago.

如您現在所見,我有一個圖像下載下傳到我的本地存儲庫中,大小約為15 GB。 您還可以注意到,該映像已由Microsoft在14個月前在docker hub中釋出/建立。

Now comes the fun part, we can issue below command to create and run the SQL container. Think of running a container is similar to installing a SQL Instance.

現在是有趣的部分,我們可以發出以下指令來建立和運作SQL容器。 想到運作容器類似于安裝SQL執行個體。

“docker run --name mssqltrek-con1 -d -p 1433:1433 -e sa_password=My$eCurePwd123# -e ACCEPT_EULA=Y microsoft/mssql-server-windows-developer”
           
  • -e: Set the -e :将ACCEPT_EULA variable to “Y” to accept end user license agreement and use ACCEPT_EULA變量設定為“ Y”以接受最終使用者許可協定,并使用SA_PASSWORD to Specify your own strong password that meets SQL Server complex password requirements SA_PASSWORD來指定您自己的強密碼,以滿足SQL Server複雜密碼要求
  • -p: Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, SQL Server is listening on TCP 1433 in the container and this is exposed to the port, 1433, on the host -p :将主機環境上的TCP端口(第一個值)與容器中的TCP端口(第二個值)映射。 在此示例中,SQL Server正在偵聽容器中的TCP 1433,并将其暴露給主機上的端口1433
  • –name: Specify a custom unique name for the container rather than a randomly generated one. If you run more than one container, you cannot reuse this same name –name :為容器指定一個自定義的唯一名稱,而不是随機生成的名稱。 如果您運作多個容器,則無法重複使用此相同名稱
  • d: Run the container in detached mode. Since SQL runs as a service, it is recommended to use this option. If not, container will take over your cmd/shell when it starts d :以分離模式運作容器。 由于SQL作為服務運作,是以建議使用此選項。 否則,容器将在啟動時接管您的cmd / shell
Windows Server 2016上具有Docker容器SQL Server

Great, Let’s verify the status of the newly created Docker windows container to make sure it’s running. For that you can issue either “Docker ps” or “Docker container ls” commands.

很好,讓我們驗證新建立的Docker Windows容器的狀态以確定其正在運作。 為此,您可以發出“ Docker ps ”或“ Docker container ls ”指令。

Tip: “Docker ps” command will only list containers which are running currently, if you want to list all the containers, issue “Docker ps -a” command.

提示: “ Docker ps ”指令将僅列出目前正在運作的容器,如果要列出所有容器,請發出“ Docker ps -a ”指令。

Windows Server 2016上具有Docker容器SQL Server

Now, Let’s create few more Windows based SQL containers based on the same image. As you can see from the below screenshot, I was able to setup/install three SQL Server Instances using Docker windows container technology in matter of few seconds as opposed to several minutes or even hours per our traditional methods for setting up SQL Instances.

現在,讓我們基于同一映像建立更多基于WindowsSQL容器。 從下面的螢幕快照中可以看到,我能夠使用Docker Windows容器技術設定/安裝三個SQL Server執行個體,而在短短的幾秒鐘内,而不是傳統SQL執行個體設定方法中的幾分鐘甚至幾小時。

Windows Server 2016上具有Docker容器SQL Server

Now let’s get the IP addresses of these Docker windows SQL containers using the below shown command, I need this information to connect to my SQL container from the host machine or remotely using SSMS or any other client tools.

現在,使用下面顯示的指令擷取這些Docker Windows SQL容器的IP位址,我需要此資訊以從主機或使用SSMS或任何其他用戶端工具遠端連接配接到我SQL容器。

  1. docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" mssqltrek-con1

Windows Server 2016上具有Docker容器SQL Server

For whatever reason, if you see any issues with starting containers, you can review the logs by issuing the command “Docker container logs –name of the container”. FYI, below are the logs of my first container mssqltrek-con1.

無論出于何種原因,如果您在啟動容器時遇到任何問題,都可以通過發出指令“ Docker容器日志–容器名稱 ”來檢視日志。 僅供參考,以下是我的第一個容器mssqltrek-con1的日志。

Windows Server 2016上具有Docker容器SQL Server

Now that we have confirmed containers are up and running, let’s connect from SSMS.

現在,我們已經确認容器已啟動并正在運作,讓我們從SSMS連接配接。

Windows Server 2016上具有Docker容器SQL Server
  • Note: By default, Docker containers for windows will install a default Instance of SQL Server. You can use localhost (if you are connecting from the same host) or the IP address or the container name along with the port numbers in order to connect to respective SQL container.
  • 注意:預設情況下,用于Windows的Docker容器将安裝SQL Server的預設執行個體。 您可以使用localhost(如果要從同一主機進行連接配接)或IP位址或容器名稱以及端口号,以連接配接到相應SQL容器。

清理Docker容器環境: (Cleaning up Docker container environment:)

Now, let’s cleanup our environment by removing all the Docker SQL containers which we did created so far. To remove/delete a container, issue the command “docker rm containername”.

現在,讓我們通過删除到目前為止建立的所有Docker SQL容器來清理環境。 要删除/删除容器,請發出指令“ docker rm containername ”。

  • Note: Containers should be in stopped state in order to remove/delete them gracefully.注意:容器應處于停止狀态,以便可以正常地删除/删除它們。
Windows Server 2016上具有Docker容器SQL Server

As you can see, I ran into errors when I attempt to remove my Docker windows container when it is in running state.

如您所見,當我嘗試在運作狀态下删除Docker Windows容器時,我遇到了錯誤。

I stopped my Docker windows containers using command “docker stop containername” and then I was able to remove them without any issues as you can see below.

我使用指令“ docker stop containername ”停止了Docker Windows容器,然後能夠删除它們而沒有任何問題,如下所示。

Windows Server 2016上具有Docker容器SQL Server

結論: (Conclusion:)

In this article, we have seen how to install containers on Windows Server 2016 from scratch and Install the latest stable version of SQL Server leveraging Docker containers technology. Container technology is here to stay and It’s not the future, it’s present. Several companies are already leveraging Docker containers technology in their shops or at least evaluating it. Getting familiar with deploying and provisioning containers could almost become a mandatory skill in near future especially for DBAs and DevOps. Hope this article will be useful in setting up Docker windows containers in a lab environment to get familiar with and explore the technology.

在本文中,我們已經了解了如何從頭開始在Windows Server 2016上安裝容器以及如何利用Docker容器技術安裝SQL Server的最新穩定版本。 容器技術将持續存在,而不是未來,而是現在。 一些公司已經在他們的商店中利用Docker容器技術,或者至少對其進行了評估。 在不久的将來,尤其是對于DBA和DevOps,熟悉部署和配置容器幾乎成為一項必不可少的技能。 希望本文對在實驗室環境中設定Docker Windows容器有所幫助,以熟悉和探索該技術。

翻譯自: https://www.sqlshack.com/sql-server-with-a-docker-container-on-windows-server-2016/

繼續閱讀