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.

打开服务器管理器 ,然后导航到“ 添加角色和功能 ”,然后选择“ 容器 ”功能,如下所示。

Server Manager - Dashboard

Selecting installation type in Add roles and features wizard

server selection in add roles and features wizard

select containers feature in add roles and features wizard

Select “Containers” feature and click “Next”.

选择“ 容器”功能,然后单击“ 下一步 ”。

Confirm installation selections in Add roles and features wizard

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

Install docker microsoft provider

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即可继续。

Install and import Nuget provider

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.

就我而言,我没有在服务器上安装必备补丁程序,因此我的安装遇到了以下屏幕截图所示的问题。

failed installing docker because of missing required updates for windows server 2016

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

好吧,让我们应用Windows Server 2016调用“ sconfig ”的最新和最大更新。

Installing updates on windows server from server configuration

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

几秒钟后,您将进入下面的屏幕,从中可以选择“ Option 6 ”以下载并安装最新更新。

select "Download and install updates"  from server configuration

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.

此时,它提示我选择安装“ 所有更新 ”或“ 仅推荐更新 ”。 如下面的屏幕截图所示,我选择安装所有更新。

Select to install all the updates

Windows downloading all the available updates

Restart the machine after installing updates

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

Installing docker containers provider

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

Start docker engine service

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安装相关的信息。

verifying docker container information

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

发出“ Docker版本”以获取版本详细信息,如下所示。

Verify the docker version

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 。 它将为我们提供所有可用命令的长长列表,如下所示。

listing all the commands available in docker.

在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

search SQL Server images available in docker hub by micrrosoft.

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 ”。 如您所见,目前我还没有。

verify available images in local registry.

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”.

pulling an image from docker hub

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 ”命令以检查情况是否有所不同。

Verify docker images in registry

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

create docker container using docker-pull.

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 ”命令。

verify the status of docker container from powershell.

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实例设置方法中的几分钟甚至几小时。

Create multiple containers  based on the same SQL image.

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容器。

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

Inspect IP address of the containers.

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的日志。

Review Docker container logs.

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

现在,我们已经确认容器已启动并正在运行,让我们从SSMS连接。

Connect to SQL Instances created in containers from remote SSMS.
  • 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.注意:容器应处于停止状态,以便可以正常地删除/删除它们。

Removing the containers gracefully.

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容器,然后能够删除它们而没有任何问题,如下所示。

Stop and remove SQL containers in windows.

结论: (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/

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值